gpt4 book ai didi

android - 如何清除 (RecyclerView) SortedList?

转载 作者:太空狗 更新时间:2023-10-29 16:35:36 26 4
gpt4 key购买 nike

我有一个 SortedList,我用它来表示我的回收站 View 数据,但我正在努力“清除”API 调用之间的数据。有什么帮助吗?我目前只是像这样遍历列表:

for(int i = 0; i < mList.size(); i++){
removeItemAt(i);
}

这似乎不一致地删除了一些项目?

提前致谢! :)

最佳答案

如果您查看 source code ,您正在做的事情的问题是 SortedList 在调用 removeItemAt 索引时更改了大小。因此 mList.size() 将在您的 for 循环迭代时发生变化,从而导致不一致的结果。

这是一种从 recyclerview SortedList 中删除项目的方法。

public void clear() {
mList.beginBatchedUpdates();
//remove items at index 0 so the remove callback will be batched
while (mList.size() > 0) {
mList.remove(mList.get(0));
}
mList.endBatchedUpdates();
}

关于android - 如何清除 (RecyclerView) SortedList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29832084/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com