Android:过滤列表视图时检查了错误的项目
我遇到了与此问题相同的问题:错误的项目检查时在android中过滤ListView
正如上面的问题所建议的,我有一个包含所有selectedId的哈希集,但我不知道当光标重新填充选中的项目时如何使用它。
我的问题只是装饰性的 - 例如:
- “Facebook”位于未过滤列表中的第五个位置。
- 用户搜索“face”,只有“Facebook”出现在过滤列表的第一个位置。
- 用户选中“Facebook”作为选择并返回到未过滤列表。
- 选中的项目是列表中的第一项,而不是“Facebook”(位于第五位)。
注意: 除了这个问题,其他都很好。 例如,“删除”将删除正确的项目,因为我使用 selectedId 来执行此操作(即使选中的项目是错误的)。
单击列表项:
OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//gets the Bookmark ID of selected position
Cursor cursor = (Cursor)parent.getItemAtPosition(position);
String bookmarkID = cursor.getString(0);
boolean currentlyChecked = checkedStates.get(position);
checkedStates.set(position, !currentlyChecked);
if (!selectedIds.contains(bookmarkID)) {
selectedIds.add(bookmarkID);
selectedLines.add(position);
} else {
selectedIds.remove(bookmarkID);
selectedLines.remove(position);
}
}
};
在光标内: - 这就是问题所在。
这会重新填充选中的项目 - 问题是它是按位置(pos)执行的并且该项目在已过滤列表中的正确位置不是其在未过滤列表中的位置 - 导致错误标记的项目。
CheckedTextView markedItem = (CheckedTextView) row.findViewById(R.id.btitle);
markedItem.setChecked(checkedStates.get(pos));
将不胜感激任何帮助!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
难道只是因为你的ListView不知道数据集发生了变化?在适配器上调用notifyDatSetChanged()以使其知道。
这将强制使用更新的数据重新绘制列表。
Could it be simply that you ListView does not know the data set changed? Call notifyDatSetChanged() on your adapter to let it know.
This will force a redraw of the list, with the updated data.
解决了问题。
正如我添加到我的问题中所建议的,当您填充复选框时,其他列表/哈希集应确定是否将项目标记为已选中。
使用我的代码:
真的希望这对任何人都有帮助! :)
Solved the issue.
As suggested in the question I added to mine, when you populate the checkboxes, the other List/Hashset should determine whether to mark item as checked or not.
Using my code:
Really hope this will help anyone! :)
这是我的解决方案:
要在过滤后在列表视图中选择一个项目,一开始,我得到了错误的项目,因为我使用了这个:
正确的方法应该是这样的:
要在过滤后删除一个项目,我已经尝试过这个:
但这不起作用,因为我的自定义适配器。在我的自定义适配器中,要使用过滤器,我有两个列表项:
因此要删除一个项目,我在自定义适配器中添加了新方法:
最后在列表视图中删除一个项目:
希望有帮助
This is my solution:
To selecte an item in list view after filtering, at the beginning, I got wrong item because I used this:
The correct way should be like this:
To delete an item after filtered, I have tried this:
But that didn't worked because my custom adapter. In my custom adapter, to use filter, I have two listItems:
So to delete an item, I added new method in my custom adapter:
Finally to delete an item in list view:
Hope this help