Android Development Tutorial--About adapter The content of the adapter has changed problem analysis

Source: Internet
Author: User

When we do Android list development, we sometimes encounter problems like this:
Java.lang.IllegalStateException:The content of the adapter have changed but ListView do not receive a notification. Make sure the content of your adapter are not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifydatasetchanged () while its content changes.
Basically, the content of your adapter changes, but your listview doesn't know it. Make sure your adapter data is changed in the main thread!

Check the Android source code to see where the exception is thrown out.
The Layoutchildren () method in the ListView has the following method:
Handle the empty set by removing all views that is visible
And calling it a day
if (Mitemcount = = 0) {
Resetlist ();
Invokeonitemscrolllistener ();
Return
} else if (mitemcount! = Madapter.getcount ()) {
throw new IllegalStateException ("The content of the adapter have changed but"
+ "ListView did not receive a notification. Make sure the content of "
+ "Your adapter is not modified from a background thread, and only"
+ "from the UI thread. [In ListView ("+ getId () +", "+ getclass ()
+ ") with Adapter (" + madapter.getclass () + ")]");
}

This means that the exception is thrown when the data count in the ListView cache is not equal to Adapter.getcount () in the ListView.

Knowing the reason, it's better to change it.
is when you do adapter within the item to increase, delete, be sure to synchronize the Notifydatasetchanged () method to notify the update. And also in the main thread.
So this problem is solved!

Android Development Tutorial--About adapter The content of the adapter has changed problem analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.