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 (mItemCount == 0) {
                resetList();
                invokeOnItemScrollListen er();
                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产生,或者尽量减少动画
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值