Dynamic Refresh of ListView-notifyDataSetChanged does not work, notifydatasetchanged

Source: Internet
Author: User

Dynamic Refresh of ListView-notifyDataSetChanged does not work, notifydatasetchanged

Many developers, especially Android beginners (I am also a beginner), do not use notifyDataSetChanged when refreshing ListView dynamically. Sometimes it is quite painful.

In fact, you still need to pay attention to the details when using yydatasetchanged. For example:

The data source used by my ListView is from ArrayList. Generally, we create an ArrayList that has the data we want to provide to the ListView. For example:

1 ArrayList<Integer> list  = new ArrayList<Interger>;2 list.add(1);

Here, our list is directed to a heap memory. Next we want to change the data in the list. At this time, we need to note that, for example, we have another ArrayList in the code called newlist, which is created using the following code:

1 ArrayList<Integer> newlist  = new ArrayList<Interger>;2 list.add(2);

At this time, newlist points to a different heap memory from list. At this time, we directly use list = newlist to hand over the newlist data to list. Here, in fact, I just changed the point of the list so that it also points to the memory pointed to by newlist. At this time, we can no longer call the yydatasetchanged method to notify listview to update data. The reason is very simple: the point of list has changed, the call to yydatasetchanged will check whether the data pointed to by the original list has changed. However, the list is no longer the original list, so it cannot judge and can only make no response. Then, the solution to this problem is to directly change the data on the original list, and you can use the following code to operate:

1 list.clear();2 list.addAll(newlist);3 adapter.notifyDataSetChanged();

In this way, the point of the list will not change, and it will still point to the original memory, so that calls yydatasetchanged () can work.

Conclusion: The focus here is that the point of the list to be passed to the ListView at the beginning cannot be changed, and the point must be directed to the same memory from memory, in this way, after calling notifyDataSetChanged, the list data changes can be detected, triggering the re-drawing of ListView!

 

Original article: http://blog.csdn.net/wuzhipeng1991/article/details/38820647

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.