isItemChecked always returns the opposite of what it is supposed to when inside an onItemClickListener?
Asked Answered
M

1

8

I currently have a ListView, which activates a Contextual ActionBar in the ListView's OnItemLongClickListener.

I am trying to make it so that items can be selected by clicking on them, but only when the Contextual ActionBar is up.

The problem is, when I check isItemChecked() , so as to toggle the item's selection state, it always returns the opposite of what it is supposed to.

Here is how I've implemented the OnItemClickListener:

list.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (mActionMode != null){
            list.setItemChecked(position, !list.isItemChecked(position));
        }
        else{
            list.setItemChecked(position, false);
        }

    }

});

EDIT: This is pretty bizzare.. this code toggles the selection state:

list.setItemChecked(position, list.isItemChecked(position));

What is going on!?

EDIT 2: Ah, it looks like android is automatically checking and unchecking each item on its own... is there any way to change this behavior and handle it myself?

Murcia answered 28/2, 2013 at 7:20 Comment(1)
We need to understand what is in the mActionMode method, as it is triggering whether the checkbox should be checked or not. Are you able to include the code for the mActionMode() method? Thanks.Argentine
E
1

here is the documentation for setItemChecked method: Sets the checked state of the specified position

in the line

list.setItemChecked(position, !list.isItemChecked(position));

you are explicitly setting it to the opposite of what isItemChecked returns by negating the secoend arguement in the statement

Edbert answered 22/11, 2015 at 7:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.