您的位置:首页 > 移动开发 > Android开发

The content of the adapter has changed but ListView did not receive a notification的解决方案

2016-07-28 09:33 423 查看
Android开发过程中,使用ListView时,发现这个错误偶尔会出现。特别是做压力测试的时候,不停的点击刷新,更容易出现这个错误。在网上找了一下关于这个问题的解决办法,说的是直接备份一份数据源,我根据他们说的,办法是可行的,上代码。
public void setDataList(ArrayList<Map<String, Object>> dataList) {
if (dataList != null) {
mDatas = (List<Map<String, Object>>) dataList.clone();
notifyDataSetChanged();
}
}

public void clearDataList() {
if (mDatas != null) {
mDatas.clear();
}
}
if (mNotificationAdapter == null) {mNotificationAdapter = new AdpFragNotificationList(mActivity);mNotiSwipeView.setAdapter(mNotificationAdapter);}mNotificationAdapter.setDataList(mNotiList);
在刷新的时候,调用setDataList()方法,这只是一种迂回的方法,后来发现本质的原因,还是因为在刷新适配器的时候,调用接口之前,清空了数据源的内容,然后再填充适配器,导致的数据源与适配器不匹配。上代码。
mPromotionSwipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {@Overridepublic void onRefresh() {mPromotionList.clear();getDataList(false, 0);}});
只需要将数据源中的清除放到更新适配器前清除即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息