问题
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