java.lang.IllegalStateException

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. ...

ListView遇到频繁更新数据源并显示时报错

adapter里面的数据更新(content)不能在在后台,否则会概率性的报错,报错一般因为更新数据源和notifyDataSetChanged不在一个线程上

一些解决方法:

对线程进行管理,如果当前Actitivty暂停了,及时停止这些线程。

数据更新后,要及时使用notifyDataSetChanged()方法通知UI,避免出现数据不一致的情况。

数据的更新,最好放在主线程中进行。这样可以使用同步数据更新与通知内容更新部分的代码。

在用到多线程的时候,可以对数据做缓存处理, 比如与ListView绑定的数据存储在ArrayList (dataList), 在线程中先将数据加入到临时ArrayList(tmpList) , 最后在调用notifyDataSetChanged()方法通知UI更新之前, 把临时ArrayList(tmpList)中的数据更新到ArrayList(dataList)中, 然后清空临时ArrayList(tmpList)数据。

/*
相对于原来,我做了两项改动:

1.将所有数据“完全”保存在adapter内部,即使有外部数据进入,也会用.clone()重新生成副本,保证了数据完全是由adapter维护的。

2.保证所有setDeviceList()/clearDeviceList()是从主线程里调用的,如何保证是从主线程中调用的呢:

  a.调用Activity.runOnUIThread()方法;

  b.使用Handler(其实这并不非常准确,因为Handler也可以运行在非UI线程);

  c.使用AsyncTask。
*/
private class DeviceAdapter extends BaseAdapter {  
  
    private LayoutInflater inflater;  
    private ArrayList<Device> devices;  
  
    public DeviceAdapter() {  
        inflater = LayoutInflater.from(mContext);  
    }  
  
    @SuppressWarnings("unchecked")  
    public void setDeviceList(ArrayList<Device> list) {  
        if (list != null) {  
            devices = (ArrayList<Device>) list.clone();  
            notifyDataSetChanged();  
        }  
    }  
  
    public void clearDeviceList() {  
        if (devices != null) {  
            devices.clear();  
        }  
        notifyDataSetChanged();  
    }  
  
    @Override  
    public int getCount() {  
        return devices == null ? 0 : devices.size();  
    }  
//以下略...






转载于:https://my.oschina.net/yaly/blog/422168

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值