一个android列表的适配器数据异步加载的问题
来源:互联网 发布:mac diva 编辑:程序博客网 时间:2024/05/17 06:16
一个android列表的适配器数据异步加载的问题
问题现象:
当点击一个ListView的子项,等更新ListView完成时,再点击一下ListView的子项,程序运行良好;
当点击一个ListView的子项,还没来得及更新ListView时,再点击一下ListView的子项,程序弹出“异常终止”提示框并崩溃。
出错Log:
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。
程序背景:
类InfoAdapter继承于BaseAdapter,其数据成员ArrayList<Info>用于存储ListView的数据。类LoadInfoThread继承于Thread,用于收集ListView的数据,设置ArrayList<Info>实例。在线程LoadInfoThread中调用notifyDataSetChanged函数通知ListView更新界面。
参考网上的文章:
1、《Adapter报错:The content of the adapter has changed but ListView did not receive a》,http://gundumw100.iteye.com/blog/1738829;
2、The content of the adapter has changed but ListView did not receive a notificat 》,http://blog.sina.com.cn/s/blog_3e333c4a01011kpd.html;
原因分析:
当线程LoadInfoThread设置完ArrayList<Info>实例,并调用完notifyDataSetChanged函数之后,ListView尚未更新时,符合Log的描述“The content of the adapter has changed but ListView did not receive a notification”(Adapter改变了但ListView尚未接收到通知)。这个时候可考虑不响应用户对ListView的操作。使用ListView.setEnabled方法避免ListView的Touch事件即可达到这个目的。
解决方法:
每次开启(start)线程LoadInfoThread之前,通过ListView.setEnabled方法设置ListView不可用,当线程LoadInfoThread设置完ArrayList<Info>实例,并调用完notifyDataSetChanged函数之后,再通过ListView.setEnabled方法设置ListView可用。
验证结果:
这样处理之后,避免了程序弹异常框和崩溃的问题。
- 一个android列表的适配器数据异步加载的问题
- Android 列表数据适配器获取当前焦点选项的索引的问题。
- 由于Android列表视图复用到之后的Imageview异步加载图片时候闪烁问题
- 图片的异步加载和适配器
- android异步加载遇到的问题
- js异步下拉列表,两级联动的一个问题?
- Android异步加载数据或图片问题
- Android解决ScrollView异步加载数据后发生滚动的问题
- Android列表显示_数据后台异步加载
- listView的异步加载数据
- 异步加载数据的方法
- android中listview的数据的异步加载
- HttpURLConnection一个适配器加载图片的类
- android列表显示的SimpleAdapter适配器
- android开发异步加载网络数据AsyncTask的使用
- android listview 滚动时异步加载图片的问题
- android listview 滚动时异步加载图片的问题
- Android ListView图片异步加载时,图片错位的问题
- 数据库集群技术漫谈
- 服务器负载均衡的基本功能和实现原理
- Dome of Circus - UVa 1473 几何 凸包
- 常见的连续概率密度函数
- C++ 多重继承,虚析构确定
- 一个android列表的适配器数据异步加载的问题
- 网站开发流程
- Leetcode: String to Integer (atoi)
- html+css第二课
- 用户密码的存储与 Python 示例
- Android中AsyncTask详解.
- 机顶盒的层次结构
- 密码传输问题
- 黑马JAVA知识点总结