Invalid item checked when filtering ListView in android

I have a ListView with several choices, where some of them are checked from the very beginning, using

setItemChecked(position, true); 

I combine this with the filter option

 setTextFilterEnabled(true) 

therefore, it is easy to find a specific entry in the list.

The problem is that when I filter the list, the input positions switch as a list. Take, for example, three entries: “A”, “B”, and “C”, where “C” is checked in advance (that is, the entry at position 3 in the list). When I type “C” on the keyboard, only the “C” entry is displayed (optional). Now “C” is no longer checked, as the record has moved from position 3 to unchecked position 1 in the list.

This behavior leads to some not very pleasant effects in the application. Is there a way to "move the selection using filtering", i.e. Bind the checked state to the record, and not to its initial position in the list? Or do I need to find a new approach?

Thanks,

Linus

+5
android checkbox listview filtering
source share
2 answers

Save the list of selected elements and whenever the list is re-populated, check if the id (not position) exists in the list and is set as selected.

+3
source share

Yes, this behavior is due to the fact that the ListView saves the position as a link to the selected elements. This means that even for adapters with stable identifiers, the choice is tied to the position (which was filtered in your case). As suggested, make some custom card / dial settings for storing identifiers that have been verified and sent them to the adapter.

0
source share

All Articles