The content of the adapter has changed but ListView did not receive a notification

来源:互联网 发布:电脑修复软件 编辑:程序博客网 时间:2024/06/07 16:50
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.Make sure your adapter calls notifyDataSetChanged() when its content changes.

此错误是由于绑定在Adapter的List中的数据增加了,而没有刷新ui导致的;并不是由于在网络线程(非UI线程)中调用add集合元素而导致的异常
你可以使用Timer模拟在非UI线程中调用add()集合元素,注意:add()过程中添加日志输出,在控制台中可以很正常的看到log一次次的打印;而真正导致此异常的原因是由于你在add()过程中点击或者触摸到了某一条目的时候没有刷新UI也就是没有调用Adapter的notifyDataSetChanged(),之所以有人说是由于在非UI线程add()元素才导致的异常,是因为在add()过程中调用notifyDataSetChanged(),而此方法需要在UI线程中进行

个人认为导致异常原因大概有两点

  • 增加元素确实没有调用notifyDataSetChanged()
  • 增加元素之后的操作太多(比如for循环嵌套,向数据库插入大量数据等待操作)并没有下一步立即执行notifyDataSetChanged();

只要在add元素之后保证notifyDataSetChanged()就不会有此异常发生了

阅读全文
0 0
原创粉丝点击