将 CheckedTextViews 的 ListView 与过滤器一起使用

发布于 2024-11-16 12:22:55 字数 465 浏览 3 评论 0原文

我正在尝试编写一个充满 CheckedTextViews 的 ListView 并使用过滤器让用户快速搜索ListView (由带有自定义 ArrayAdapter 的数组支持)。我已经设置了 XML 中声明列表视图的 multipleChoice 标志和 I 不使用过滤器时可以选中/取消选中多个条目。什么时候 使用过滤器搜索条目,我可以选择一个条目。 但是,当我清除过滤器(通过按退格键)时,条目位于 尽管不再是同一位置,但仍会检查该位置 我想要检查的实际条目。

我尝试在 CheckedTextView 上手动设置选中状态 在适配器的 getView() 方法中,但这似乎不是 在职的。只是为了笑,我尝试将 isChecked 设置为 false ListView 中的每个 CheckedTextView,但是 CheckedTextView 在使用过滤器时进行了检查,保持检查状态。

有没有人有幸实施这样的事情或有任何 建议?

谢谢 亚当

I am trying to write a ListView that is filled with CheckedTextViews
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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安稳善良 2024-11-23 12:22:55

您可能想查看 ListView#clearChoices()。 ListView 本身根据适配器项目的位置而不是项目的 id 来维护选择。因此,如果位置发生变化(例如,通过过滤),则选择位置基本上不再有效。

它还维护一个已检查 ID 的列表,您可以使用 ListView#getCheckedItemIds(),并且只要您的适配器具有稳定的 id(即,特定 id始终 引用相同的概念项,并且不随项目的顺序或过滤而变化),您可以使用它在过滤更改时恢复选中的位置(您必须迭代整个适配器才能将 id 与位置匹配)。

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).

世界和平 2024-11-23 12:22:55

我正在查看 ListView.clearChoices() ,但我希望还有另一种方法。我最终要做的是调用 ListView.clearChoices()< /a> 每当过滤器更改时,手动跟踪已选择的内容,并调用 ListView.setItemChecked() 来自 ArrayAdapter .getView()。这使我不必一直迭代整个列表。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文