修复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(2131296509, class android.widget.ListView) with Adapter(class com.ah.delivery.adapter.HomeAdapter)]错误问题

1.首先看出现错误的源码定位

ArrayList中数据放在adapter.setData中出现The content of the adapter has changed but ListView did not receive_adapter 通知和数据不一致

从上面可以看出mItemCount与mAdapter.getCount()不相等

mAdapter.getCount是adapter中的数据长度

mItemCount是item的数量,也就是布局中的item数量

两个不相等说明数据与界面不同步

2.根据以上结论排查是数据与界面不同步

数据是adapter.setData中设置数据

界面是通过notifyDataSetChange()等其他方式更新的

所以得出结论setData中的数据与notifyDataSetChange()不一致

3.根据上面排查出setData与notifyDataSetChange不同步,不一致

ArrayList中数据放在adapter.setData中出现The content of the adapter has changed but ListView did not receive_adapter 通知和数据不一致_02

如上看起来是一致的,其实内在是可能不一致,因为list可能可变,要让其不变

ArrayList中数据放在adapter.setData中出现The content of the adapter has changed but ListView did not receive_adapter 通知和数据不一致_03

如上所示如果dataList改变则上面的list会改变,最后导致数据不同步,所以要写成下面的写法。

ArrayList中数据放在adapter.setData中出现The content of the adapter has changed but ListView did not receive_adapter 通知和数据不一致_04

如上所示当dataList改变adapter中的list不会改变,完美解决