Question

I have added a checkbox to the Listview and managed to get the checkbox listener using a custom adapter. Everything is working except that the listview item click Listener is not getting focused at all.

Here is my Code:

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

}

Was it helpful?

Solution

Can you try adding the following line in the XML where you have declared the checkbox :

    android:focusable="false"

I had encountered this myself and after disabling the focus everything worked well.

Let me know if it works out for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top