Home » Android » Android content of the adapter has The changed but...

Android content of the adapter has The changed but ListView did not receive a

in Android development, ListView very much, whether novice or veteran of many years of development for many years, are likely to encounter this exception:



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. Make sure your adapter calls notifyDataSetChanged (when) its content changes.



The abnormal reason is that your data set is changed, but does not call notifyDataSetChanged () method, but many people are very confused, it is called the notifyDataSetChanged () method, but reported the exception, I also developed more than two years of Android, or encountered this problem, I may be not enough strength, there is no rest this weekend, to solve this problem, it pays off, and finally solved
.
Let me describe the scene where this problem occurred:

Most of the time, we get the data from the network, and when we get it, we change the local data source, and then call the notifyDataSetChanged () method, and this process is not wrong.

We obtain the data will not directly access the UI thread inside the general, we will open a new thread, or with AsyncTask, there is some third party framework, such as okhttp, I use is okhttp, regardless of the approach, a new thread is in the run () method. AsyncTask is in the doInBackground () method, okhttp (onResponse) is in the method, we get the data are acquired in the inside of the thread, we will get the data changes directly with the binding of Adapter data set, the data set is changed in this thread inside, but we can't thread which calls notifyDataSetChanged () method, we must be on the UI thread which calls notifyDataSetChanged () method, then we will use the Handler (or runOnUiThread) method to deal with the problem, this time to, We change the data set in the thread, but called on the UI thread inside the notifyDataSetChanged () method, which is why the problem occurred, this anomaly does not necessarily occur from time to time, but will occasionally appear, once the error will not be prompted in one line, all internal abnormalities, and if you use the abnormal report third party, also won't be reported, is very difficult, very difficult to troubleshoot
.
the solution is to change the data set and call notifyDataSetChanged () method must not separate in the UI thread, and, according to the order, change the data set before, immediately call the notifyDataSetChanged () method, !!!

Latest