The content of the adapter has changed but ListView did not receive a notification 异常解决方案

来源:互联网 发布:阿里云空间登陆 编辑:程序博客网 时间:2024/06/06 09:57

收藏己用,方便以后再遇到此问题解决~~

因为在非Ui线程中修改了ListView绑定的数据对象。这个同样会造成在非UI线程中更新主线程控件的错误。解决方法如下:

在Thread中将数据存入一个临时对象中,如下:
                tempAppList.add(tmpInfo);//之前是直接将 mAppsList 在这个地方用,系统就会检测到主线程中Listview绑定的数据源在改变,所以会报错

                Log.i(TAG, "initAppsList send a message to show listview");
                Message message = new Message();
                message.what = MainActivity.SEND_INIT_LIST_MSG;
                message.obj = tempAppList;
                mHandler.sendMessage(message);


然后在主线程中定义的mHandler中进行数据的绑定和更新,如下:
                        case SEND_INIT_LIST_MSG:
                                Log.i(TAG, "handleMessage");
                                //特别注意,在非UI线程下不能够对主线程中listview绑定的list数据进行修改。只能创建一个temlist,然后在
                                //handle里面赋值。
                                mAppsList = (ArrayList<AppInfo>) msg.obj;
                                mAppsAdp = new AppsListAdapter(mContext, mAppsList);
                                lv_apps.setAdapter(mAppsAdp);
                                mAppsAdp.notifyDataSetChanged();
0 0
原创粉丝点击