The content of the adapter has changed but ListView did not receive a notification
2014-09-21 13:41
447 查看
在非Ui线程中修改ListView绑定的数据对象的问题
问题描述:
在非UI线程中修改了ListView绑定的数据对象(如List)时,如下异常:
问题分析:
在非UI线程下不能够对主线程中ListView绑定的List数据进行修改。
解决方法:
方法一:将对List中数据的修改放到主线程中。
方法二:将子线程中要更新的数据通过Handler传递到主线程中,然后在主线程中将传递过来的数据添加到ListView绑定的数据对象List中,然后同时adapter数据改变。
在这里主要介绍第二种方式:
具体步骤:
1.在子线程中将要更新的数据以Message的方式传递给主线程。
2.在主线程中通过Handler处理子线程发来的消息,更新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();
相关文章推荐
- The content of the adapter has changed but ListView did not receive a notification 错误的解决方案
- 【遇见Android bug】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的问题
- Android开发-- The content of the adapter has changed but ListView did not receive a notification - With AsyncTask
- 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,java.lang.IndexOu
- Android - Bug: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
- .解决bug: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异常
- 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 错误的解决方案
- 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