How to invalidate ListView for new data when using ListAdapter

纵饮孤独 提交于 2019-12-11 21:16:47

问题


I am making use of a default listview in a fragment to show data that is being called from a URL. There is a SwipeRefreshLayout where the ListView is contained. However when I refresh, the old data is still in the listview and the new data is just added at the top of the list. Is there a way to invalidate the old data?
I have made use of a ListAdapter which is created from the SimpleAdapter class like so:
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);

I have tried to make the adapter null or use the invalidate() method that comes with the listview. The adapter does not have a notifyDataSetChanged() method.
The newsItemList is an ArrayList that has the data from the URL. It is declared like so:
ArrayList<HashMap<String,String>> newsItemList;


回答1:


the important is : do you clear ArrayList> newsItemList when you get new data? if u do just repeat:

      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); 



回答2:


Try using adapter.clear(); put it in the method that is executed when you perform a refresh!



来源:https://stackoverflow.com/questions/32792394/how-to-invalidate-listview-for-new-data-when-using-listadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!