I already spent a lot of time trying to solve this problem, I’ve seen a lot other StackOverflow Posts about this topic but I couldn’t find a solution that worked for me.
I have a ListActivity and a custom ArrayAdapter. There are several buttons in my listview for example a delete button. However the onItemClick method would only be invoked if the TextView of the list is clicked. I can add OnClickListeners to the buttons, but then I have the problem that I don’t know the position of the element to which the button belongs. I know I could set tags for every single button, but there has to be a better way of doing it!
This is my adapter:
private class ItemListAdapter extends ArrayAdapter<Item> {
private ArrayList<Item> items;
private int countItems = 0;
public ItemListAdapter(Context ctx, int textViewResourceId, ArrayList<Item> itemList){
super(ctx, textViewResourceId, itemList);
this.items = itemList;
this.countItems = itemList.size();
}
public int getCount(){
return countItems;
}
public Item getItem(int pos){
return itemList.get(pos);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
//Get the current list item
final Item currentItem = itemList.get(position);
if (row == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = vi.inflate(R.layout.itemlist_item_row, null);
}
Item item = items.get(position);
if (item != null){
TextView bt = (TextView) row.findViewById(R.id.bt_itemlist_item);
if (bt != null){
bt.setText(item.getName());
}
}
return row;
}
}
Would be great if someone could tell me the best solution for this.
Set the
OnClickListenersforButtonsingetView()and you’ll get the position