Adapter refresh data pitfall, Adapter refresh data
When the adapter refresh data, it must be able to refresh successfully, and ensure that each refresh changes the data source.
So I did this and wrote in the constructor of the adapter:
private List<ListBean> listItems = new ArrayList<>(); private Context mContext; public ResultAdapter(Context context, List<ListBean> listItems) { super(context); this.mContext = context; this.listItems = listItems; }
Call the method in the adapter when refreshing the data: (this method is used to change the data source in the adapter every time to ensure that the data is successfully refreshed. This idea is correct. However, I made a mistake, causing data to be cleared after refreshing)
/*** Refresh data ** @ param listItems * @ param keyWord */public void setData (List <ListBean> listItems) {if (listItems! = Null) {this. listItems. clear (); this. listItems. addAll (listItems) ;}notifydatasetchanged ();}
What errors have I made?
My constructor is wrong. In the constructor of the adapter, I use
This. listItems = listItems;
In this way, the listItem passed in points to the same object as the listItem In the adapter. In the method of refreshing data, I used
if (listItems != null) { this.listItems.clear(); this.listItems.addAll(listItems); }
The listItems In the adapter is cleared, and the listItem passed in externally is also cleared, so this. listItems. addAll (listItems); it is still empty, and no response is returned after the data is refreshed.
Reprinted please indicate the source: http://www.cnblogs.com/tangZH/p/8535653.html