异常 The content of the adapter has changed but ListView did not receive a notification可能的一种解决方法
来源:互联网 发布:python的应用范围 编辑:程序博客网 时间:2024/05/18 16:39
最近后台异常信息中总是出现这个异常:
STACK_TRACE=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. [in ListView(16908298, class com.eebbk.common.list.PullToRefreshSwipeListView$InternalListViewSDK9) with Adapter(class android.widget.HeaderViewListAdapter)]
大致看一下,意思是说适配器 adapter 中的数据发生了变化,但是 adapter 没有收到通知,原因可能是 adapter 的数据修改操作是在非 UI 线程里面完成的,或者是 adapter 数据修改操作完成后,没有通知 adapter 更新数据;
我的 Android 项目遇到的问题是,由于数据删除操作可能很耗时所以必须另开一个线程去做删除操作,不允许阻塞 UI 线程,删除完成后又要在 UI 线程里面 notify adapter 去更新数据,这样的话数据删除操作和更新数据操作分别在两个线程,且必须要保证删除完成立即更新,我的做法是,无论你操作的数据对象是保存在 Activity 中还是在 Adapter中,你在做删除操作时将该数据对象加锁,例如:
点击删除按钮,按以下步骤执行:
1.更新UI,显示正在删除的提示;
2.新开线程删除File,保存 File 的 DataList 集合同步 Remove 数据,并且将该 List 集合加锁(synchronized),不熟悉的可以百度一下线程锁的具体用法;
3.然后 RunOnUIThread,或者利用 Handler.sendMessage,通知适配器重新setAdapter(DataList),并且执行adapter.notifyDataSetChanged();
4.更新 UI,显示删除完成的提示;
如果不加锁的话,由于删除操作和更新操作不在同一个线程,很有可能非 UI 线程还没删除完成,UI 线程就已经通知adapter更新数据了,因此加锁可以保证更新操作是在删除操作完成之后再去做;
以上只是我解决问题过程中的一点点个人总结,点滴积累,贵在坚持,感谢你的阅览!
- 异常 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 异常解析
- 处理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 异常解决
- 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终极解决方法
- 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.
- 大学生走出校园踏入社会需坚持的原则
- div垂直水平居中的五种方法
- 史上最全编程语言列表_你掌握了哪些?
- 【Lua】Mac系统下配置SublimeText的Lua编译环境
- 二叉树非递归遍历
- 异常 The content of the adapter has changed but ListView did not receive a notification可能的一种解决方法
- Vue.js系列之vue-resource
- 前端知识谱图
- python第16篇之关键参数赋值(keyword)
- ESP8266 sdk开发入门
- [笔记]: 背包问题
- 设计模式分类与六大原则
- springboot学习9之admin监控配置
- 20170613