"Go" About ListView notifydatasetchanged () Refresh data not updated cause

Source: Internet
Author: User

When using the ListView: When you want to dynamically display the changed data (such as database changes), many people should have used notifydatasetchanged (); This method refreshes the ListView to show the changed data.

It's time to pay attention:
......
Private arraylist......
userslist= query (...); Gets a ArrayList based on the query function and assigns a value to the data source bound to adapter Userslist
Sadapter.notifydatasetchanged ();
......

If you are also: the above-mentioned form to refresh the ListView data, it is not updated. Must be changed to the following form:

......
userslist= query (...);
Userslist.clear ();
Userslist.addall (Query (...));
Sadapter.notifydatasetchanged ();

///////////////////////////////////////////////////////////////////////////////////////

The reason is: Sadapter gets the contents of the list through userslist. But it's possible (and should) be that when you call Super (Myactivity.this, R.layout.item, userslist) Sadapter saves a reference to the original list (assumed to be List a) in Userslist. After calling the query function, Userslist points to a new list (List B). However, when calling Notifydatasetchanged (), the Sadapter will be updated with a saved reference (that is, a reference to List a), so of course the original result will not be updated.


If this happens, you can use the latter to refresh the ListView, it should be okay.

"Go" about notifydatasetchanged () refresh data in the ListView is not updated cause

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.