打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
The content of the adapter has changed but Li...

The content of the adapter has changed but ListView did not receive a notification终极解决方法

分类: Android安卓应用开发 1930人阅读 评论(2) 收藏 举报

这几天做一个自动扫描SD卡上所有APK文件的小工具,扫描过程中会把APK添加到LISTVIEW中显示,结果出现以下错误:(有时候触摸更新数据时候,触摸listview也会报错)

  1. 10-26 18:30:45.085: E/AndroidRuntime(7323): java.lang.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. [in ListView(2131296280class android.widget.ListView) with Adapter(class com.souapp.appmanager.ApkListAdapter)]  

10-26 18:30:45.085: E/AndroidRuntime(7323): java.lang.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. [in ListView(2131296280, class android.widget.ListView) with Adapter(class com.souapp.appmanager.ApkListAdapter)]

其实我在listview的adapter添加完数据后,使用了handler去调用datper.notifyDataSetChanged();来通知listview显示变化结果;

虽然自己很确定没有多线程操作,但是有人说listview本来就是线程不安全的,这个不关心了,看了国外一个开发者的方法很简单:

  1. ListView.requestLayout();  
  2. Adatper.notifyDataSetChanged();  

在你adpater更新前,调用listview的requestLayout(),这样做无非就是拟补数据数量不一致导致报错,虽然一个解决的好办法。

但是实际上用的时候我发现也会出问题,想了想最彻底的解决办法:

把  listview的adapter数据更新和dapter.notifyDataSetChanged()必须同时放到单独一个线程里,报错基本是都是这个原因,有人把adapter里的数据更新了,但是 dapter.notifyDataSetChanged()     放到一个单独线程去更新,结果出现notifyDataSetChanged更新同步的问题


题外话:解决问题还是从本质上思考,从错误信息上提示,一定避免多线程去更新adapter的数据,为此我把多线程递归遍历SD卡目录修改成了单线程;

有的时候看到别人的一些补救方法未真正解决问题。



1
0
查看评论
1楼 xiaochongzi716717 3天前 14:20发表 [回复]
你这句话“把 listview的adapter数据更新和dapter.notifyDataSetChanged()必须同时放到单独一个线程里,报错基本是都是这个原因,有人把adapter里的数据更新了,但是 dapter.notifyDataSetChanged() 放到一个单独线程去更新,结果出现notifyDataSetChanged更新同步的问题
”真不知道说的是什么意思,可能你懂了,可我看的是一头雾水!
不过自已想明白了出错的原因。
Re: 心有灵犀鬼才心 前天 17:48发表 [回复]
回复xiaochongzi716717:其实就是一句话:
adapter数据更新和dapter.notifyDataSetChanged()要放在一起执行,最好放在一个handle里
当adpater数据发生了改变,马上执行dapter.notifyDataSetChanged()
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Adapter报错:The content of the adapter has changed
android ListView 数据更新的总结
关于Adapter的notifyDataSetChanged是否生效
Android 面试题总结之Android 基础(五)
ListView中的观察者模式
台式机硬件常识(买机前一定要看)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服