The content of the adapter has changed but ListView did not receive a notification终极解决方法
来源:互联网 发布:java web start 5.0 编辑:程序博客网 时间:2024/05/16 11:39
这几天做一个自动扫描SD卡上所有APK文件的小工具,扫描过程中会把APK添加到LISTVIEW中显示,结果出现以下错误:(有时候触摸更新数据时候,触摸listview也会报错)
- 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)]
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本来就是线程不安全的,这个不关心了,看了国外一个开发者的方法很简单:
- ListView.requestLayout();
- Adatper.notifyDataSetChanged();
在你adpater更新前,调用listview的requestLayout(),这样做无非就是拟补数据数量不一致导致报错,虽然一个解决的好办法。
但是实际上用的时候我发现也会出问题,想了想最彻底的解决办法:
把 listview的adapter数据更新和dapter.notifyDataSetChanged()必须同时放到单独一个线程里,报错基本是都是这个原因,有人把adapter里的数据更新了,但是 dapter.notifyDataSetChanged() 放到一个单独线程去更新,结果出现notifyDataSetChanged更新同步的问题
题外话:解决问题还是从本质上思考,从错误信息上提示,一定避免多线程去更新adapter的数据,为此我把多线程递归遍历SD卡目录修改成了单线程;
有的时候看到别人的一些补救方法未真正解决问题。
- The content of the adapter has changed but ListView did not receive a notification终极解决方法
- The content of the adapter has changed but ListView did not receive a notification终极解决方法
- The content of the adapter has changed but ListView did not receive a notification终极解决方法
- ListView:The content of the adapter has changed but ListView did not receive a notification终极解决方法
- listview提示The content of the adapter has changed but ListView did not receive a notification
- ListView异常:The content of the adapter has changed but ListView did not receive a notification
- ListView The content of the adapter has changed but ListView did not receive a notification.
- The content of the adapter has changed but ListView did not receive a notification.
- The content of the adapter has changed but ListView did not receive a notification
- 解决bug:The content of the adapter has changed but ListView did not receive a notification
- The content of the adapter has changed but ListView did not receive a notification
- The content of the adapter has changed but ListView did not receive a notification.
- The content of the adapter has changed but ListView did not receive a notification
- .解决bug:The content of the adapter has changed but ListView did not receive a notification .
- 处理The content of the adapter has changed but ListView did not receive a notification异常
- The content of the adapter has changed but ListView did not receive a notification错误分析
- The content of the adapter has changed but ListView did not receive a notification 异常解析
- 处理The content of the adapter has changed but ListView did not receive a notification异常
- Java如何获取文件编码格式
- hdu4004
- The content of the adapter has changed but ListView did not receive a notification 错误的解决方案
- 开始我的博客生涯~
- 数据库范式(1NF 2NF 3NF BCNF)详解一
- The content of the adapter has changed but ListView did not receive a notification终极解决方法
- 监控storm 的 作业收发数据情况
- git-daemon
- 生成ssh key
- 十大前端开发框架(下)
- Android JNI如何将Java和Native对应
- Jquery mobile的动态化
- hadoop 使用log4j日志
- 如何:使用 SQL Server 数据库项目或服务器项目进行调试