[Project summary] on Android Adapter notifydatasetchanged and notifydatasetinvalidated Invalid reason

Source: Internet
Author: User

Recently encountered a problem in the development, adapter use notifydatasetchanged and notifydatasetinvalidated invalid, after thinking and online search, the following reasons are obtained.

First look at the difference between notifydatasetchanged and notifydatasetinvalidated

We can apply the Granary to explain the difference between the two.

Notifydatasetchanged: There is less food in the granary, or more, send a notice.

notifydatasetinvalidated: Granary changed, such as the original from a warehouse to take food, now replaced by B granary.

Next, look at the references to objects and objects in Java.

Use the new method in Java to create an object. Assign the address of the object to a variable at the same time.

What you need to understand is that this variable is not an object, but rather a variable that stores the address of the object. This is a bit around.

Or the Granary, the new object, is equivalent to creating a granary. And the address of this barn is the object variable you declared.

The object in Java can be said to have no life cycle, because he only uses the existence and destruction of two states. In other words, objects are always present after they are created, until they are reclaimed by the system. He will not destroy it as you declare the variable to be destroyed.

Having understood the above two points, let us analyze why the two methods in adapter do not work.

Or use a granary to make a metaphor.

With Granary A, we need to show the food in a.

Then, one day the transport team, brought in a batch of grain, this batch of food stored in two ways.

1. Put the grain in a, which is the Add method of the list we are talking about.

2. Create a barn B to put the grain in the B granary. Is the code, the new List--->add.

Analysis of these two situations, why does not work.

First, if the use of notifydatasetinvalidated, it will not work, because the data source has not changed, is the Granary is still in. Notifydatasetchanged should be used at this time. The grain in the Granary has changed.

The second, which is the most complex, most of them do not work in this case.

Analysis: The reason why notifydatasetchanged does not work is because food in a granary has not changed. Adapter remains a reference to the address of a granary.

The reason notifydatasetinvalidated does not work is because the adapter variable remains a reference to a, without changing the reference address.

Workaround: In the first case, there is basically no problem.

The main is the second: if the data object is a new object, and the reference is assigned to the list to be displayed, notify the adapter that the data source has changed. For example, re-setlist () and use notifydatasetinvalidated.

In fact, all of the reasons are the understanding of objects and references in Java. Have time to write an example again.

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.