Maarten
Maarten

Reputation: 7318

Why does getCheckedItemPositions() return the inverse truth values

I have a ListView in multiple selection mode. Whenever I click an item, I want to handle that event. For this, I use the following logic.

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    OverlayTypes selected = (OverlayTypes) getListAdapter().getItem(
            position);
    boolean isChecked = getListView().getCheckedItemPositions().valueAt(
            position);
    Log.i(TAG, position+" is "+isChecked);
}

But whenever an item gets checked, getChecked is 'false' and 'true' vice versa. Why?

Upvotes: 0

Views: 322

Answers (2)

Maarten
Maarten

Reputation: 7318

My problem was that I was overly ambitious in get the getView() method of my custom adapter. In getView(), I tried to set the Checkable view to the value that it has in my settings, not realizing that checked state is handled within the adapter.

If that makes sense.

Upvotes: 0

biddulph.r
biddulph.r

Reputation: 5266

It might be an issue with timing, the value of the checked box may not be updating until after you have printed out to the LogCat.

Try changing your line

boolean isChecked = getListView().getCheckedItemPositions().valueAt(position);

to

boolean isChecked = l.getCheckedItemPositions().valueAt(position);

to see if reading the local copy instead of your global copy helps.

Upvotes: 1

Related Questions