【发布时间】:2014-03-10 19:53:37
【问题描述】:
我有 ListFragment 和一个自定义适配器。
我列表中的每一行都包含两个 TextView 和一个 ImageButton。
我想在单击图像按钮时调用一个方法。
public class MyCustomAdapter extends SimpleAdapter {
private final Context context;
private final List<? extends Map<String,? >> data;
private final String[] from;
public MyCustomAdapter(Context context, List<? extends Map<String, ?>> data, String[] from) {
super(context, data, R.layout.list_item, from, new int[]{R.id.firstLine, R.id.secondLine});
this.context=context;
this.data=data;
this.from=from;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
TextView firstLine = (TextView) rowView.findViewById(R.id.firstLine);
TextView secondLine = (TextView) rowView.findViewById(R.id.secondLine);
firstLine.setText((CharSequence) data.get(position).get(from[0]));
secondLine.setText((CharSequence) data.get(position).get(from[1]));
ImageButton imageButton = (ImageButton) rowView.findViewById(R.id.tourItemDelete);
final long tourId = (Long) data.get(position).get("key");
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//do something dependend on tourId
}
});
return rowView;
}
我想在我的 ListFragment 中有这个 onClickListener
我的 ListFragment 中的 onListItemClick 方法仅在我单击两个 TextView 之一时调用,但在单击 ImageButton 时不调用。
- 是否可以将此 onClickListener 从我的适配器移动到 ListFragment? tourId 也应该从我的适配器传递到 ListFragment 中的方法。
- 为什么单击 ImageButton 时未调用 ListFragment 中的 onListItemClick 方法?
【问题讨论】:
标签: android listview onclicklistener