The content of the adapter has changed but ListView did not receive a notification, listviewadapter
Modifying the Data Object bound to the ListView in a non-Ui thread
Problem description:
When a Data Object (such as List) bound to the ListView is modified in a non-UI thread, the following exception occurs:
Problem Analysis:
The List data bound to the ListView in the main thread cannot be modified in a non-UI thread.
Solution:
Method 1: place the changes to the data in the List in the main thread.
Method 2: Pass the data to be updated in the Child thread to the main thread through Handler, and then add the transmitted data to the List of data objects bound to the ListView in the main thread, then, the adapter data changes at the same time.
The second method is introduced here:
Procedure:
1. The data to be updated in the Child thread is transmitted to the main thread as a Message.
Map <String, Object> item = new HashMap <String, Object> (); // obtain the program package name String packageName = appInfo. packageName; item. put ("imgIco", ico); item. put ("appName", appName); item. put ("packageName", packageName); Message message = new Message (); message. what = SCANNING; message. obj = item; mHandler. sendMessage (message );
2. Handler is used in the main thread to process messages sent by sub-threads and update the data object of ListView.
Handler mHandler = new Handler () {@ Overridepublic void handleMessage (Message msg) {// TODO Auto-generated method stubswitch (msg. what) {case FLAG_LOAD_SUCCESS: // scan break; case SCANNING: // SCANNING items. add (Map <String, Object>) msg. obj); // notify the adapter to change the adapter data. notifyDataSetChanged ();
ListView often has this problem. How can this problem be solved?
When the data in your listview changes, you must notify the adapter to tell it that the data has changed:
Adapter. notifyDataSetChanged ();
Because this method is actually updating the UI, it must be performed in the UI thread, that is, the main thread.
When android loads data on The next page, the error message "The content of the adapter has changed" is displayed for multiple slides during stress tests.
The updated Adapter data must be added in the UI thread, and notifications using the notify function are added. Or paste the source code.