CheckBox won't toggle in Android ListView with focusable="false"

Posted by user3563124 on Stack Overflow See other posts from Stack Overflow or by user3563124
Published on 2014-05-31T15:22:21Z Indexed on 2014/05/31 15:25 UTC
Read the original article Hit count: 315

Filed under:
|
|
|

I'm creating a ListView with a custom Adapter for displaying my entries. Each row contains a checkbox, and my adapter contains the following code:

        CheckBox cb = (CheckBox) v.findViewById(R.id.item_sold);
        cb.setChecked(p.isSold);
        setupCheckboxListener(cb, v, position);
            ...

private void setupCheckboxListener(CheckBox check, final View v, final int position) {
    check.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckBox cb = (CheckBox) v;
            if (cb.isChecked()) {
                System.out.println("Should become false!");
                cb.setChecked(false);
            } else {
                System.out.println("Should become true!");
                cb.setChecked(true);
            }
        }
    });

My row XML file includes the following:

    <CheckBox android:id="@+id/item_sold"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.10"
        android:layout_gravity="center"
        android:gravity="center"
        android:focusable="false"
        android:clickable="false"/>

But anytime I press one of the checkboxes, check.isChecked() returns true, even if the box is unchecked. I even checked to make sure that the checkboxes were distinct and weren't picking up just the last value/etc.

Setting up the listeners inline instead of in a method doesn't seem to help, either. It's literally just the isChecked() condition that isn't working appropriately - it seems to always give me the inverse value.

Setting an onClick on the row is not acceptable in this case because I need to allow row selection for something else.

What could be causing this issue?

© Stack Overflow or respective owner

Related posts about android

Related posts about listview