Source: Internet
Author: User

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. ...

The ListView encounters frequent updates to the data source and displays the Times wrong

Adapter inside the data Update (content) can not be in the background, or will be probabilistic error, error generally because the update data source and notifydatasetchanged is not on a thread

some workarounds:

threads are managed, and if the current actitivty is paused, these threads are stopped in a timely manner.

after the data is updated, use the Notifydatasetchanged () method to notify the UI in a timely manner to avoid inconsistent data.

update the data, preferably on the main thread. This allows you to use synchronous data to update the code for the part of the notification content update.

when multithreading is used, data can be cached, such as the data that is bound to the ListView is stored in ArrayList (DataList), and the data is first added to the temporary ArrayList (tmplist) in the thread. Finally, before calling the Notifydatasetchanged () method to notify the UI update, update the data in the temporary ArrayList (tmplist) to ArrayList (DataList). The temporary ArrayList (tmplist) data is then emptied.

/* Relative to the original, I made two changes: 1. All data is "fully" stored inside the adapter, and even if there is external data entry, it is used. Clone () to regenerate the copy, ensuring that the data is fully maintained by adapter.  2. Ensure that all setdevicelist ()/cleardevicelist () are called from the main thread, how to ensure that it is called from the main thread: A. Call the Activity.runonuithread () method; B. Use handler (this is not very accurate, because handler can also run on non-UI threads); c. Use Asynctask.      */private class Deviceadapter extends Baseadapter {private Layoutinflater inflater;        private arraylist<device> devices;      Public Deviceadapter () {inflater = Layoutinflater.from (Mcontext); } @SuppressWarnings ("Unchecked") public void Setdevicelist (arraylist<device> list) {if (list!              = null) {devices = (arraylist<device>) list.clone ();          Notifydatasetchanged ();          }} public void Cleardevicelist () {if (Devices! = null) {devices.clear ();      } notifydatasetchanged ();      } @Override public int getcount () {return devices = = null? 0:devices.size (); }//The following slightly ...


Related Article

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: 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.