views:

55

answers:

3

The app has a ListView with multiple-selection enabled, in the UI it works as expected. But when I read the values out using this code:

Log.i(TAG,"Entered SearchActivity.saveCategoryChoice()");
SparseBooleanArray checkedPositions = categorySelector.getCheckedItemPositions();
Log.i(TAG,"checkedPositions: " + checkedPositions.size());
if (checkedPositions != null)
{
    int count = categoriesAdapter.getCount();
    for ( int i=0;i<count;i++)
    {
        Log.i(TAG,"Selected items: " + checkedPositions.get(i));
    }
}

I get this output, no matter what state each checkbox is in:

Entered SearchActivity.saveCategoryChoice()
checkedPositions: 0
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false

The SparseBooleanArray seems to return false for any non-existent item, so the source of the problems seems to be that getCheckedItemPositions() is returning an empty array. The method is behaving as if there are no items in the ListView, but there are.

I can see from the docs that no values are returned when the ListView is not set up as multi-select, but it is, using this statement:

categorySelector.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

In my scenario, the adapter I'm using is a subclass of ArrayAdapter, and (without any solid evidence) I suspect this may be the cause, though I can't see why it shouldn't work.

A: 

Isn't there a rather fundamental difference between 'selected' and 'checked'?

Suspect you want to setItemChecked() from an OnItemSelectedListener...

Reuben Scratton
Ollie C
Yes, it's a bit confusing why they made the selection mode relevant at all. Having looked through ListView.java just now, it seems to me that unless you call setItemChecked() yourself I don't think you will get the automatic checked state toggling that you are (quite reasonably) assuming should happen when the item is selected. The only automatic checked state toggling that I can see happens when list items are added, and only then if your View-derived class implements Checkable. It would be good if default selection handling did the same thing...
Reuben Scratton
I suppose that makes sense, but still pretty confusing. If I have a multiple-selection ListView with checkboxes and there's a method called getCheckedItemIds() it seems reasonable to assume it will provide the status. But I've reimplemented it using a handler on OnCheckedChangeListener and that's working fine, in fact using getTag() it's a nice and clean approach. Thanks for sharing your thoughts.
Ollie C
A: 

I remember having an issue with this myself a while back. Here is my previous question, which isn't directly related to your issue, but contains some code that may help. What you might want to try is using checkedPositions.valueAt(int index) rather than checkedPositions.get(int index). I think that may be what you're actually looking for.

kcoppock
I just tried using valueAt() and for me, for whatever reason, it returns false for all of them, no matter what the checkboxes are set to.
Ollie C
A: 

I still do not know why, but in my scenario, getCheckedItemPositions() returns false values for all items. I cannot see a way to use the methods on the ListView to get the boolean values out. The SparseBooleanArray object seems to have no real-world data in it. I suspect this may be because of some quirk of my implementation, perhaps that I've subclassed ArrayAdapter. It's frustrating, issues like this are a real time-drain.

Anyway, the solution I have used is to to attach a handler to each Checkbox individually as ListView rows are created. So from ListView.getView() I call this method:

private void addClickHandlerToCheckBox(CheckBox checkbox)
{
    checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
        CheckBox checkbox = (CheckBox)arg0; 
        boolean isChecked = checkbox.isChecked();
        // Store the boolean value somewhere durable
    }
      }
);
Ollie C