Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong item checked when filtering ListView in android

I have a ListView with multiple choice entries where some are checked from the beginning using

setItemChecked(position, true);

I combine this with the filtering option

setTextFilterEnabled(true)

so it's easy to find a specific entry in the list.

The problem is that when I filter the list the entries switch position in the list view. Take for example three entries: 'A', 'B' and 'C' where 'C' is checked beforehand (i.e. the entry at position 3 in the list). When I type a 'C' on the keyboard, only the 'C' entry is displayed (as intended). Now 'C' is not checked anymore, since the entry has moved from the checked position 3 to the unchecked position 1 in the list.

This behavior leads to some not very welcome effects in the app. Is there a way to "move the selection with the filtering", i.e. bind the checked state to the entry and not to its initial position in the list? Or do I need to find a new approach?

Thanks,

Linus

like image 434
aspartame Avatar asked Mar 21 '10 16:03

aspartame


1 Answers

Maintain a list of the selected items, and whenever the list is repopulated check if that id (not position) exists in the list, and set as selected.

like image 108
Pentium10 Avatar answered Nov 01 '22 02:11

Pentium10