Adapter refresh data pitfall, Adapter refresh data

Source: Internet
Author: User
Tags addall

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.