我已经花了很多时间试图解决这个问题,我已经看到了很多关于这个主题的其他 StackOverflow 帖子,但我找不到适合我的解决方案。
我有一个 ListActivity 和一个自定义 ArrayAdapter。我的列表视图中有几个按钮,例如删除按钮。但是,只有在单击列表的 TextView 时才会调用 onItemClick 方法。我可以将 OnClickListeners 添加到按钮,但是我遇到的问题是我不知道按钮所属元素的位置。我知道我可以为每个按钮设置标签,但必须有更好的方法!
这是我的适配器:
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;
}
}
如果有人能告诉我最好的解决方案,那就太好了。