Botão no ListView usando ArrayAdapter
Eu tenho um ArrayAdapter que é preenchido usando uma classe POJO. O listview é composto por 2 layout. 1 é para um item de menu e um é para a categoria. O Listview com o separador está bem.
Mais tarde, tentei adicionar um botão em cada linha de menuitem para editar os detalhes nele. Aqui eu tenho um problema quando tentei obter a posição da linha onde o botão foi clicado.
Eu tentei mostrar a posição usando o log. 1. Se houver menos número de linhas e não houver necessidade de rolar. o log mostra a posição correta. 2. Se eu tiver mais linhas em execução em páginas, a posição no meu log não está correta.
Você poderia, por favor, me guiar até a linha onde meu código precisa ser corrigido? Desde já, obrigado
public class ConfirmAdapter extends ArrayAdapter<POJO_ConfirmMenu> {
private ArrayList<POJO_ConfirmMenu> ticketItem;
Context context;
LayoutInflater vi;
public ConfirmAdapter(Context context ,ArrayList<POJO_ConfirmMenu> menu) {
super(context, 0, menu );
this.ticketItem = new ArrayList<POJO_ConfirmMenu>();
this.ticketItem.addAll(menu);
this.context =context;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
private class ViewHolder {
TextView ticketItem;
TextView type;
TextView quantity;
Button cancel,edit;
}
public boolean isEnabled(int position) {
if (ticketItem.get(position).getItemType().equals("menucategory"))
return false;
return true;
}
public int getItemViewType(int position) {
if (ticketItem.get(position).getItemType().equals("menucategory"))
return 0;
return 1;
}
public int getViewTypeCount() {
return 2;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case 0:
convertView = vi.inflate(R.layout.group, null);
holder.ticketItem = (TextView) convertView.findViewById(R.id.tvGroup);
convertView.setBackgroundColor(Color.RED);
break;
case 1:
convertView = vi.inflate(R.layout.confirmitem, null);
holder.ticketItem = (TextView) convertView.findViewById(R.id.tvConfirmItem);
holder.quantity = (TextView) convertView.findViewById(R.id.tvQuantity);
holder.cancel = (Button) convertView.findViewById(R.id.bCancel);
holder.edit = (Button) convertView.findViewById(R.id.bEdit);
holder.edit.setTag(position);
// Edit button
holder.edit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int pos = (Integer) v.getTag();
Log.i("ConfirmAdapter ","Order Edit @ position : " + pos);
}
});
break;
} convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
switch (type) {
case 0:
holder.ticketItem.setText(ticketItem.get(position).getTicketItemObject().getCategoryName()) ;
convertView.setBackgroundColor(Color.RED);
break;
case 1:
holder.ticketItem.setText(ticketItem.get(position).getTicketItemObject().getName());
holder.quantity.setText(Integer.toString(ticketItem.get(position).getTicketItemObject().getItemCount()));
break;
}
return convertView;
}
}
}