SoFunction
Updated on 2025-04-09

Solution to the exception of Android ListView

Android ListView exception solution:

ListView: The content of the adapter has changed but ListView did not receive a notification When using ListView, the following exception information is encountered:

10-26 18:30:45.085: E/AndroidRuntime(7323): : The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131296280, class ) with Adapter(class )]

Actually, after I added data to the adapter of the listview, I used the handler to call(); to notify the listview to display the change results;

Although I am very sure there is no multi-threading operation, some people say that listview is originally thread-insecure, and I don’t care about this anymore. It’s very simple to see a foreign developer’s method.

Solution 1:

(); 

();

Before you update the adpater, call the requestLayout() of the listview. This is nothing more than making up for the inconsistency in the number of data, which is a good solution.

But when I actually used it, I found that there would be problems. I thought about the most thorough solution.

Thorough solution:

The listview adapter data update and the () method call must be placed in a separate thread at the same time. This is basically the reason for the error. Some people updated the data in the adapter, but () was placed in a separate thread to update, and the problem of notifyDataSetChanged update synchronization occurs.

Analysis and summary of problems that occur when updating ListView data:

If you haven't read it carefully, you will say that the data of ListVIew is updated in a non-UI thread, and then subconsciously think that the call to the method is placed on a non-UI thread. When you look carefully, it means that the data of the ListView and the notification data should be placed on the same thread (main thread). In order to keep the data consistent, an array will be stored in the adapter. The modification of that data and calling the notifyDataSetChanged method should be put together, and it will be placed on the main thread. If the update of the data is placed on the child thread, the call to notifyDataSetChanged will bind the ListView to the main thread by default. At this time, if the child thread updates the data, it will appear in the non-UI thread to modify the UI thread.

This problem does not necessarily crash. There are many lower versions, but there are fewer crashes in the higher versions.

Thank you for reading, I hope it can help you. Thank you for your support for this site!