Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is ListView.getCheckedItemPositions() not returning correct values?
    primarykey
    data
    text
    <p>The app has a <strong>ListView</strong> with multiple-selection enabled, in the UI it works as expected. But when I read the values out using this code:</p> <pre><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&lt;count;i++) { Log.i(TAG,"Selected items: " + checkedPositions.get(i)); } } </code></pre> <p>I get this output, no matter what state each checkbox is in:</p> <pre><code>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 </code></pre> <p>The <a href="https://developer.android.com/reference/android/util/SparseBooleanArray.html" rel="nofollow noreferrer">SparseBooleanArray</a> 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.</p> <p>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:</p> <pre><code>categorySelector.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload