很多新手在Android开发时可能对Thread和Message以及Handler、Looper概念不是很清楚,可能产生类似 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.的错误。在这里给出以下几点可以帮助你了解该问题:
1.改变适配器Adapter内容时不要在后台线程中,必须在UI线程中处理,这点可以通过Handler传出来解决。
2.尝试Adapter的notifyDataSetChanged()方法,当然主要看你代码的具体情况。
以上只貌似只是把错误信息翻译过来而已,真正的原因请看源代码:
mItemCount = mAdapter == null ? 0 : mAdapter.getCount();
..... //中间是其他代码就不拷贝了
else if (mItemCount != mAdapter.getCount()) {//这里可以看出为什么会抛出adapter has changed,是因为你可能还有线程在跑,更新了mAdapter.getCount();的返回值
throw new 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(" + getId() + ", " + getClass()
+ ") with Adapter(" + mAdapter.getClass() + ")]");
============
ps:知其然,知其所以然。刚开始我发现写不写notifyDataSetChanged()都能实现效果,于是我就把它注释了,直到出了问题,我看了以上内容,我就明白为什么会有adapter has changed的提示,也明白了notifyDataSetChanged()的含义。
我的理解是:增加了内容长度改变了 但是没有通知更新 当选中增加的那个内容时 就造成了 数组越界(或者别的问题)
所以添加内容后要更新。
分享到:
相关推荐
Android 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. M
Android自定义adapter的listview代码
android adapter适配器详解
Android之Adapter用法总结
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...
Android ListView adapter不同布局,当需要用到ListView叠加嵌套时候可以考虑
android recyclerview adapter 照片选择器 给recyclerview 添加adapter适置器进行照片选择,可以对recyclerview进行行数和张数的设置,也可以对已选择的照片进行增减。
android gridview adapter 实例 每个getview显示不同的数据
本代码深入讲解了Android适配器各种对象的使用,适合初学者学习。
让你更好地理解android adapter的特色和应用范围。
android listview的一个例子。使用了java的反射功能,实现自动构建adapter,不需要自己从BaseAdapter继承
挺不错的,很详细的介绍了Adapter的各个用法
android adapter(listview gridview等的)学习建议和一些注意点
Android listview与adapter操作和用法
Android_Adapter 讲解Android_Adapter 讲解Android_Adapter 讲解Android_Adapter 讲解Android_Adapter 讲解
这个是我写的一个自定义Adapter适配器,用来绑定数据到ListView上。我写的代码非常简单易懂
这个是在网上找到的一个对adapter的封装,别人封装的很好,基本很少的代码就可以写一个adapter
android中ArrayAdapter SimpleAdapter BaseAdapter 的使用
Android简单使用Listview和Adapter实现数据显示,通过按钮删除ListView中的子项
整合了网上发布的资源,这个绝对可以装好!(里面有两个文件)