我在 Listview 中添加了一个复选框,并设法使用自定义适配器获取复选框侦听器。一切正常,除了列表视图项单击侦听器根本没有得到关注。
这是我的代码:
public class MyCursorAdapter extends SimpleCursorAdapter {
private Cursor c;
private Context context;
int selectCount;
public MyCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.c = c;
this.context = context;
}
public View getView(int pos, View inView, ViewGroup parent) {
View v = inView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.row_bookmark, null);
}
c.moveToFirst();
c.moveToPosition(pos);
TextView title = (TextView) v.findViewById(R.id.title);
title.setText(this.c.getString(this.c.getColumnIndex("title")));
TextView details = (TextView) v.findViewById(R.id.details);
details.setText(this.c.getString(this.c.getColumnIndex("date")));
final CheckBox cb = (CheckBox) v.findViewById(R.id.checkBox1);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (cb.isChecked()) {
if (cb.isClickable()) {
selectCount = selectCount + 1;
}
} else if (!cb.isChecked()) {
if (cb.isClickable()) {
if (selectCount > 0)
selectCount--;
}
}
// if any item is checked display the actionMode
if (selectCount == 1) {
BookmarkActivity.startmode();
}
if (selectCount == 0) {
BookmarkActivity.stopmode();
}
if (selectCount != 0)
BookmarkActivity.mMode.setTitle(selectCount + " selected");
}
});
return (v);
}
}