调试程序,发现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的数据源,清空临时数据源。
测试了一下,问题解决了。
该问题就是需要数据源变更和更新变更永远在一起。
程序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的数据源,清空临时数据源。
测试了一下,问题解决了。
该问题就是需要数据源变更和更新变更永远在一起。