标签:
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 (mItemCount == 0) {
resetList();
invokeOnItemScrollListener();
return;
} else if (mItemCount != 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() + ")]");
}
2、mItemCount != mAdapter.getCount()?
AdapterView.class 172行
@ViewDebug.ExportedProperty
int mItemCount;//应该是上一次加载的数量,私有
3、AdapterView.class ?
ListView extends AbsListView
AbsListView extends AdapterView<ListAdapter>
4、网上主要解决方式
mAffairList.setVisibility(View.GONE);
mAdapter.notifyDataSetChanged();
mAffairList.setVisibility(View.VISIBLE);
只能解决主动通知时的情况,大多数情况是没有通知造成的
5、mAdapter.notifyDataSetChanged() 在做什么事?
点进去
class BaseAdapter
public void notifyDataSetChanged() {
mDataSetObservable.notifyChanged() ;
}
点进去
class DataSetObservable
public void notifyChanged() {
synchronized(mObservers) {
for (DataSetObserver observer : mObservers) {
observer.onChanged();
}
}
}
点进去
abstract class DataSetObserver
public void onChanged() {
// Do nothing
}
AdapterView.class中
class AdapterDataSetObserver extends DataSetObserver {
private Parcelable mInstanceState = null;
@Override
public void onChanged() { //实现方法
mDataChanged = true;
mOldItemCount = mItemCount;
mItemCount = getAdapter().getCount();//关键地方
... ...
6、理论上的解决方式
listView所加载的list是静态的,大小不会变
只要list的size()变化了,就马上发通知
7、我的建议
只要list的size()变化了,就马上发通知
对主list的size()的改变与通知更新会存在时间差,滚动listView时不改变主list大小会大大减少bug产生,或者尽量减少动画
Android The content of the adapter has changed but ListView did not receive a notification
标签:
原文地址:http://www.cnblogs.com/zhujiabin/p/5799174.html