开发者

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 lis开发者_JS百科t 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


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.


Yes, this behaviour is because ListView holds the position as a reference to selected items. This means that even for adapters with stable IDs the selection is bound to the position (which has been filtered out in your case). As suggested, implement some custom map/set for holding IDs that are checked and feed them to the adapter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜