Listview监听器在复选框添加到列表视图后不响应单击

时间:2012-10-19 15:16:46

标签: android android-listview android-checkbox

我在Listview中添加了一个复选框,并设法使用自定义适配器获取复选框侦听器。一切正常,除了listview项单击Listener根本没有集中注意力。

这是我的代码:

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);
}

}

2 个答案:

答案 0 :(得分:2)

您可以尝试在已声明复选框的XML中添加以下行:

    android:focusable="false"

我自己也遇到过这种情况,在禁用焦点之后一切运行良好。

让我知道它是否适合你。

答案 1 :(得分:1)

如果您点击此链接,您将找到所有答案:

http://android.cyrilmottier.com/?p=525