参考: http://www.pocketdigi.com/20111018/548.html
错误信息
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 the UI thread. [in ListView(2131165479, class com.wmyc.activity.com.listview.pulllistview.XListView) with Adapter(class com.wmyc.activity.com.listview.multicolumnlistview.internal.PLA_HeaderViewListAdapter)] mItemCount: 2, mAdapter.getCount(): 7
自认为代码没有写错 但是 如果 我切换activity速度非常快的话 基本上都会报这个错
我的代码如下:
private class LoadDataThread implements Runnable{ private int type; public LoadDataThread(int type){ this.type = type; } @Override public void run() { if(type==IXListViewListener.TYPE_REFRESH){ mIndexShow = 0; } ArrayList<InfoCloth> list = mDaoCloth.getAllByPage(mIndexShow, mTagId);//分页加载数据 switch(type){ case IXListViewListener.TYPE_LOADMORE: mArrData.addAll(list); break; case IXListViewListener.TYPE_REFRESH: mArrData.clear(); mArrData.addAll(list); break; } System.out.println("LoadDataThread"); if(mArrData.size()==DBOpenHelper.PAGE_COUNT){//如果达到每页显示个数 就分页 mIndexShow = mIndexShow+1; } System.out.println("1111111111"); if(list.size() < DBOpenHelper.PAGE_COUNT){ System.out.println("2222222222"); mHandler.sendEmptyMessage(MSG_FOOTVIEWGONE); } System.out.println("33333333333"); Message msg = new Message(); msg.arg1 = type; msg.what = Constant.MSG_SUC; mHandler.sendMessage(msg); System.out.println("444444444444"); } }
然后 我在handler里面 更新listview
//handler private Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(_dialog!=null && _dialog.isShowing()){ _dialog.dismiss(); } switch(msg.what){ case Constant.MSG_FAIL: break; case Constant.MSG_SUC: showData(); break; } } };
这个就是我老是报错的代码 我在线程里面 更改了adapter绑定的数据,然后再handler里面 更新listview
搜了下,发现很多人都遇到这个问题
listView1.setVisibility(View.GONE); listAdapter.notifyDataSetChanged(); listView1.setVisibility(View.VISIBLE);
说这样写可以解决 反正我这样写是没解决 可能我写的有问题吧
最后,我把ListView的添加,删除,清空等UI操作都用Handler来处理,上面的代码 稍微改下
private class LoadDataThread implements Runnable{ private int type; public LoadDataThread(int type){ this.type = type; } @Override public void run() { if(type==IXListViewListener.TYPE_REFRESH){ mIndexShow = 0; } mArrDataTemp = mDaoCloth.getAllByPage(mIndexShow, mTagId);//分页加载数据 Message msg = new Message(); msg.arg1 = type; msg.what = Constant.MSG_SUC; mHandler.sendMessage(msg); System.out.println("444444444444"); } }
新写一个临时的list集合 用于存储得到的数据
case Constant.MSG_SUC: switch( msg.arg1){ case IXListViewListener.TYPE_LOADMORE: mArrData.addAll(mArrDataTemp); break; case IXListViewListener.TYPE_REFRESH: mArrData.clear(); mArrData.addAll(mArrDataTemp); break; } System.out.println("LoadDataThread"); if(mArrData.size()==DBOpenHelper.PAGE_COUNT){//如果达到每页显示个数 就分页 mIndexShow = mIndexShow+1; } System.out.println("1111111111"); if(mArrDataTemp.size() < DBOpenHelper.PAGE_COUNT){ System.out.println("2222222222"); mFallView.setPullLoadEnable(false); } showData(); break; }
就这样 我大概测了下 暂时没再遇到这个问题