Andriod ListView线程问题

来源:互联网 发布:揭东军埔淘宝村 编辑:程序博客网 时间:2024/05/17 00:47
       调试程序,发现Listview的页面偶尔会出现异常,退出,分析一下,可能是Listview线程不安全的问题导致的。
       程序Log:

      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(2131361792, class android.widget.ListView) with Adapter(class android.widget.SimpleAdapter)] 

     说的是listview 的数据更新了,但是没有通知Listview。
     查找了一下解决方案:
     在多线程的时候,Listview的数据源变化和绑定永远在一起,就可以避免这样的问题。
     推荐的解决方案是,使用一个临时的数据源去线程接收数据。
     Listview数据变更的时候,将临时数据源的数据复制给Listview的数据源,清空临时数据源。
     测试了一下,问题解决了。
     该问题就是需要数据源变更和更新变更永远在一起
0 0