和其他许多人一样,我在使用 ListView 时遇到了 IllegalStateException :(
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
我知道基本的解释 - 我可能正在尝试从后台线程修改列表。在一遍又一遍地探索我的代码之后,我找不到我实际上正在这样做的地方。我没有添加\从列表中删除元素 - 它是静态的,并且不会因创建而改变。 我所做的只是从工作线程修改列表元素的内部数据 - 但我不确定这是否确实是我的问题。 所以 - 这是我的问题 - 只要我不通过添加\删除元素来修改列表本身,是否允许从后台线程修改列表元素的底层数据?
更新: 好吧,我想你是对的,所以这是一个伪代码:
//from main GUI thread.
ArrayList<People> namesList = // create and populate the list...
//lets assume that NamesArrayAdapter extends ArrayAdapter<People>
NamesArrayAdapter adapter = new NamesArrayAdapter(context, listViewResource, namesList); //instantiate the list adapter
//from backgroung thread
namesList.getItem(0).setName("et"); //Modifying existing element inside the list - is this ligal???
最佳答案
我所做的只是从工作线程修改列表元素的内部数据
它与您在Adapter
中设置的静态列表相同吗?
那么您就无法从后台线程
更改它们。如果您更改它们,请确保调用 notifyDataSetChanged
。
关于android - ListView 列表的 IllegalStateException 不添加\删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12902570/