Android failed to call notifydatasetchanged method. Solution

Android failed to call notifydatasetchanged method. Solution

If you use listview and GridView for data display, when the bound data is updated, you need to refresh the listview in real time, that is, call the notifydatasetchanged method of the adapter, but many people will find that the listview data is not refreshed after calling. What is the reason? It will be explained in detail below.

1. The data source is not updated. Calling notifydatasetchanged is invalid. 2. The data source is updated, but it points to a new reference. Calling notifydatasetchanged is invalid. 3. The data source has been updated, but the adapter has not received the message notification and cannot dynamically update the list.

A typical error is:

Originally, it was thought that the adapter would monitor the changes of the list. After reinitializing the list, the listview will automatically refresh the data. In fact, it is not. The adapter monitors the changes of new string [] {"listview item"}. The adapter itself will save an internal reference to the original data source (new string [] {"listview item"})_ list。

When list = new string [] {"new listview1 item"} is executed; After the statement, the list is re initialized, that is, the relationship between list and the original data source (New String[]{"listView item}} is quite cut off. Therefore, calling notifyDataSetChanged will not work after list and inner_. List is already two completely different objects that exist on the heap.

Error review:

Some time ago, arrayist was used as the original data source. Generally, it is used for operations such as add, so list and inner_ List and always keep references to the same variable, and there is no problem. Of course, if you change to direct assignment, the call will be invalid. Therefore, you should operate on the original data object instead of reassigning it.

Take a look at the source code of arrayadapter:

The above is a summary of the common reasons and solutions for Android's failure to call the notifydatasetchanged method. If you have any questions, I hope you can leave a message for discussion or go to the community of this site. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>