在使用Listview中,Listview填充的内容是从网络加载中获取数据,通常我们需要开启线程做一系列的网络处理。假设init()方法里面我已经封装好了网络操作,那么接下来我使用adapter.notifyDataSetChanged()或者不使用都会报如下错误:
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. [in ListView(2131558516, class android.widget.ListView) with Adapter(class android.widget.HeaderViewListAdapter)]
具体的原因可能是因为在init()里面开启了线程,在这个线程里面给Listview填充的一些操作还没有做完就去更新adapter的话,会出现这样的错误。
解决的方法是:把adapter.notifyDataSetChanged()是放在init()方法里面。也可以说是等这个线程结束后再去执行相关的操作。
应该还有很多解决的方法,如果各位有知道的其他解决方法我们互相学习好吗?