我正在 fragment 中使用默认 ListView 来显示从 URL 调用的数据。有一个包含 ListView 的 SwipeRefreshLayout。但是,当我刷新时,旧数据仍然在 ListView 中,新数据只是添加到列表顶部。有没有办法让旧数据失效?
我使用了从 SimpleAdapter 类创建的 ListAdapter,如下所示:
ListAdapter adapter = new SimpleAdapter(getActivity(), newsItemList, R.layout.list_item, new String[]{TAG_TITLE, TAG_DATE, TAG_OWNER}, new int[]{R.id.news_title, R.id.news_date, R.id.news_owner});
lvGeneralNews.setAdapter(adapter);
我尝试使适配器为空或使用 invalidate()
listview自带的方法。该适配器没有notifyDataSetChanged()
方法。newsItemList
是一个 ArrayList,其中包含来自 URL 的数据。它的声明如下:
ArrayList<HashMap<String,String>> newsItemList;
最佳答案
重要的是: 当你收到新数据时,你会清除ArrayList> newsItemList吗? 如果你这样做 只需重复:
ListAdapter adapter = new SimpleAdapter(getActivity(), newsItemList, R.layout.list_item, new String[]{TAG_TITLE, TAG_DATE, TAG_OWNER}, new int[]{R.id.news_title, R.id.news_date, R.id.news_owner});
lvGeneralNews.setAdapter(adapter);
关于java - 使用ListAdapter时如何使ListView对新数据无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32792394/