开发者

Using ListView of CheckedTextViews with a filter

I am trying to write a ListView that is filled with CheckedText开发者_JAVA技巧Views and uses a filter to allow the user to quickly search the ListView (which is backed by an array with a custom ArrayAdapter). I have set the multipleChoice flag in the XML that declares the listview and I can check/uncheck multiple entries when not using the filter. When using the filter to search for an entry, I am able to select an entry. However, when I clear the filter (by hitting backspace) the entry at the same position remains checked enventhough it is no longer the actual entry that I want checked.

I have tried manually setting the checked state on the CheckedTextView in the adapter's getView() method but that does not seem to be working. Just for grins, I tried setting isChecked to false in every CheckedTextView in the ListView, but the CheckedTextView that was checked while using the filter remains checked.

Has anyone had any luck implementing something like this or have any recommendations?

Thanks Adam


You might want to look at ListView#clearChoices(). The ListView itself is maintaining the choices based on position of the adapter item, not the id of the item. So if the position changes (e.g., by filtering), the choice positions are essentially no longer valid.

It does also maintain a list of checked ids, which you can retrieve with ListView#getCheckedItemIds(), and as long as your adapter has stable ids (i.e., a particular id always refers to the same conceptual item, and does not vary with the order or filtering of the items), you can use that to restore the checked positions when the filtering changes (you'll have to iterate the entire adapter in order to match up ids to positions).


I was looking at ListView.clearChoices() , but I was hoping there was another way. What I eneded up doing was calling ListView.clearChoices() whenever the filter is changed, manually keeping track of what has been selected, and calling ListView.setItemChecked() from ArrayAdapter.getView(). This prevents me from having to iterate over the whole list all the time.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜