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

解决listview的数据源更新和adapter的数据更新问题;

一般listview的数据源和adapter的数据源都是一起更新的,但是要是用异步更新listview的数据,然后在主线程调用adapter.notifydatasetchange的话就会报这个错误.

解决的办法就是要把notifydatasetchange和listview的数据源更新放在一个线程更新.

但是这样的话,listview的数据源就不能异步了,所以就在adapter上做文章.

首先,adapter的构造方法不能把listview的数据源放进去,而是用set数据源的方式


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_第1张图片

然后再adapter的getcount上面不再返回mdata.size;而是return mDatas == null ? 0 : mDatas.size();

在创建adapter的时候就要这样:

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_第2张图片

最后使用方法:

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_第3张图片

这样不管是触摸item还是快速上下拉listview都不会再报这可恶的错了;更不会cresh了

你可能感兴趣的:(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)