The content of the adapter has changed but ListView did not receive a notification

本文介绍了一种解决Android开发中ListView出现java.lang.IllegalStateException错误的方法。通过重写notifyDataSetChanged()方法并在其中更新数据集大小,确保了数据变化时ListView能够正确接收到通知。

android 新手,在开发中遇到第一次见到的一个错误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. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131296805, class android.widget.ListView) with Adapter(class cn.lingox.android.adapter.ChatAllHistoryAdapter)],查阅了好多资料也看到了android大牛的博客等 等,说的一些解决 此问题的方式, 我采用了一种比较简单的方式

重写 notifyDataSetChanged()方法,在方法中获取集合的大小,然互在getcount 中返回集合大小即可

  @Override
    public void notifyDataSetChanged() {
        size = datas.size();

        super.notifyDataSetChanged();
    }

getcount中返回集合大小

       @Override
    public int getCount() {

//        return datas.size();
        return size;
    }



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值