Android ListView 通知数据集更改
我有一个自定义列表视图,每个条目都包含旋转进度条和单行文本视图。我总是只更新列表中的第一项。问题是,每次我调用notifyDataSetChanged时,所有可见视图都会被重新创建或回收为convertView。
这种方法有两个问题:
所有条目都被重新创建,而且速度很慢 - 这不是必需的,因为我只更新第一个条目中的文本视图
ProgressBar 动画每次都会重新启动
所以我认为我只会保留对适配器中第一个 View 的引用并更新它手动。它有效,但它随机抛出 IllegalStateException(“适配器的内容已更改,但 ListView 未收到通知。请确保适配器的内容未从后台线程修改,而仅从 UI 线程修改。”
)我解决它吗?有没有办法通知 ListView 只有第一个条目发生了变化?即使有,ProgressBar动画仍然会失败。还有其他方法可以手动更新吗?
谢谢
I've got a custom listview, each entry contains spinning ProgressBar and single line textview. I'm always updating only the first item in the list. The problem is that every time I call notifyDataSetChanged all visible views are either recreated or recycled as convertView.
There are 2 problems with this approach:
all entries are recreated and that's slow - and that's not required as I'm updating only the textview in first entry
the ProgressBar animation restarts every time
So I though that I'll just keep the reference to first View in adapter and update it manually. It worked, but it randomly throws IllegalStateException ("The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread.")
How do I solve it? Is there a way to notify ListView that only first entry changed? Even if there is, ProgressBar animation will still fail. Is there any other way to update it manually?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每当我需要更新列表视图上的单个项目时,我都会使用这个:
缺点是您需要在适配器中保存对列表视图的引用。
由于此代码通常是从后台任务调用的,因此您必须使用处理程序来调用它:
Whenever i need to update one single item on a listview i use this:
The downside is that you need to hold a reference to the listView in your adapter.
As this code is usually called from a background task, you have to use a handler to call it: