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

在非Ui线程中修改ListView绑定的数据对象的问题

问题描述:

在非UI线程中修改了ListView绑定的数据对象(如List)时,如下异常:

问题分析:

在非UI线程下不能够对主线程中ListView绑定的List数据进行修改。

解决方法:

方法一:将对List中数据的修改放到主线程中。

方法二:将子线程中要更新的数据通过Handler传递到主线程中,然后在主线程中将传递过来的数据添加到ListView绑定的数据对象List中,然后同时adapter数据改变。

在这里主要介绍第二种方式:

具体步骤:

1.在子线程中将要更新的数据以Message的方式传递给主线程。

Map<String, Object>item=new HashMap<String, Object>();
//获取程序的包名
String packageName=appInfo.packageName;
item.put("imgIco", ico);
item.put("appName", appName);
item.put("packageName", packageName);
Message message = new Message();
message.what =SCANNING;
message.obj = item;
mHandler.sendMessage(message);

2.在主线程中通过Handler处理子线程发来的消息,更新ListView的数据对象。

Handler mHandler=new Handler(){
	@Override
	public void handleMessage(Message msg) {
		// TODO Auto-generated method stub
		switch (msg.what) {
		case FLAG_LOAD_SUCCESS://完成扫描

			break;
		case SCANNING://正在扫描
			items.add((Map<String, Object>) msg.obj);
			//通知适配器数据改变
			adapter.notifyDataSetChanged();
时间: 2024-10-12 08:51:46

The content of the adapter has changed but ListView did not receive a notification的相关文章

android 修改listview中adapter数据时抛出异常java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification问题

近日在做项目时遇到非必现crush,具体异常信息为: // Short Msg: java.lang.IllegalStateException // Long Msg: 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 mo

android adapter报错The content of the adapter has changed but ListView did not receive a notification.

在Android编程中使用Adapter时,偶尔会出现如下错误: 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. DDMS中的log也无法定位到准确的出错位置.检

Android 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. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread 原因:没有在主线程里通知.... 1.bug 出现的地方 listView.class 1487行 if (mIte

java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification

ListView UI重绘时触发layoutChildren, 此时会校验listView的mItemCount与其Adapter.getCount是否相同,不同报错. ListView.layoutChildren: mItemCount是在父类AdapterView中定义的,package类型 在两个地方mItemCount会被赋值,初始设置Adapter时: xxAdapter.notifyDataSetChanged时: 因此一定要确保修改adapter数据和notifyDataSetC

【转】解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题

原文网址:http://blog.csdn.net/ueryueryuery/article/details/20607845 我写了一个Dialog,Dialog中有一个ListView,想要点ListView中的一项后,跳转到另外一个Activity去. 但在使用时,会偶尔报出下面的错误: 02-21 14:54:28.928: E/AndroidRuntime(2846): FATAL EXCEPTION: main 02-21 14:54:28.928: E/AndroidRuntime

Android错误:The content of the adapter has changed...

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. 分析: 这个错误通常是listview等控件在使用adapter适配数据时可能传入的是一个静态的list,而在ad

【原创】关于Adapter的The content of the adapter has changed问题分析

关于Adapter的The content of the adapter has changed问题分析 1.问题描述 1 07-28 17:22:02.162: E/AndroidRuntime(16779): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of y

【转】关于Adapter的The content of the adapter has changed问题分析 关于Adapter的The content of the adapter has changed问题分析

原文网址:http://www.cnblogs.com/monodin/p/3874147.html 1.问题描述 1 07-28 17:22:02.162: E/AndroidRuntime(16779): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of you

Android开发教程--关于Adapter的The content of the adapter has changed问题分析

我们在做android list开发的时候,有时候会遇到类似这样的问题: 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 th