适配器内容发生变化但ListView没有收到通知
我有一个带有复选框的不同列表视图的应用程序:它们填充有 CursorAdapter。 用户可以选中/取消选中项目,并将这些项目存储到TreeSet中。 最后一个活动是一个简单的列表视图,它从 TreeSet 读取数据。
活动在选项卡中组织。
选择 TreesSet 是因为我需要对最后一个列表视图中的元素进行排序。
问题是,有时,我会收到此错误:
11-13 06:58:51.068: E/AndroidRuntime(1870): java.lang.IllegalStateException: 适配器的内容已更改,但 ListView 没有收到通知。确保适配器的内容不是从后台线程修改的,而是仅从 UI 线程修改的。 [在 ListView(2131099669, class android.widget.ListView) 和 Adapter(class org.boggix.adapter.CalcoloAdapter)]
请注意,“CalcoloAdapter”是最后一个 Activity 的适配器。
我怀疑问题在于 TreeSet 本身:它正在更改其中元素的顺序(按预期),因此最后一个活动中的适配器会抱怨它。
我尝试在 CalcoloAdapter.onResume() 内使用 notificationDataSetChanged(), 但错误在此之前出现(当我检查/取消检查其他列表视图上的元素时会发生错误)。
我怎样才能避免这种情况?
I have an app with differents listview with checkboxes: they are populated with a CursorAdapter.
The user can check/uncheck items and the items are stored into a TreeSet.
The last activity is a simple listview and it reads data from the TreeSet.
The activity are organized in tab.
The TreesSet was chosen because I needed to sort the elements in the last listview.
The problem is that,sometimes, I get this error:
11-13 06:58:51.068: E/AndroidRuntime(1870): 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(2131099669, class android.widget.ListView) with Adapter(class org.boggix.adapter.CalcoloAdapter)]
Note that “CalcoloAdapter” is the adapter of the last activity.
I suspect that the problem is the TreeSet itself: it’s changing the order (as expected) of the elements inside it so, the adapter in the last activity is complaining about it.
I tried to use notifyDataSetChanghed() inside the CalcoloAdapter.onResume(),
but the error is coming before that (it occurs when I check/uncheck elements on the others listview).
How can I avoid this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试从 UI 线程以外的线程修改适配器/ListView 会产生此问题。
因此,如果您正在新线程中执行某些操作,则检查/取消检查,然后适配器相关的工作应该仅在 UI 线程中完成。
为此使用
AsyncTask
、Handler
或RunOnUITHread
。An attemp to modify adapter/ListView from thread other that UI thread ,creates this problem .
so on Check/unckeck if you are doing some stuff's in new thread then Adapter related work should be done in UI thread only .
Use
AsyncTask
,Handler
orRunOnUITHread
for this .