Android listview exception resolution

Android listview exception resolution:

Listview: the content of the adapter has changed but listview did not receive a notification

10-26 18:30:45.085: E/AndroidRuntime(7323): java. lang.IllegalStateException: 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 android.widget.ListView) with Adapter(class com.souapp.appmanager.Apklistadapter)]

In fact, after adding data to the listview adapter, I used the handler to call datper notifyDataSetChanged(); To notify listview to display the change result;

Although I'm sure there is no multi-threaded operation, some people say that listview is inherently thread unsafe. I don't care. It's very simple to see the method of a foreign developer.

Solution 1:

ListView. requestLayout();

Adatper. notifyDataSetChanged();

Before you update adpater, call listview's requestLayout (). This is nothing more than making up for the inconsistency of data, causing a false report, though a good solution.

But in fact, when I used it, I found that there would be problems, and thought of the most thorough solution

Thorough solution:

Update the adapter data of listview and the adapter The call of notifydatasetchanged () method must be placed in a single thread at the same time. This is basically the reason for the error. Someone updated the data in the adapter, but the adapter Notifydatasetchanged() is put into a separate thread to update. As a result, the problem of notifydatasetchanged update synchronization occurs

Solve the problem when updating listview data. Analysis summary:

If you don't read it carefully, you can tell at a glance that you updated the data of listview in the non UI thread, and then subconsciously think it is calling adapter The call of notifydatasetchanged method is placed in the non UI thread. A closer look means that updating listview data and notifying data updates should be placed in the same thread (main thread) to maintain data consistency. An array is generally stored in the adapter. The modification of that data and the call of notifydatasetchanged method should be placed together and in the main thread, If the data update is placed in the child thread, the notifydatasetchanged call is in the main thread, and the notifydatasetchanged call 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 have to crash. The low version crashes more, and the high version seems to crash less

Thank you for reading, hope to 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
分享
二维码
< <上一篇
下一篇>>