【发布时间】:2015-08-10 09:56:17
【问题描述】:
在我的 android 应用程序中显示以下异常。我正在使用单个列表视图和单个适配器。我正在使用 2 个不同的回调函数(来自 signalr)来更新列表视图,但一次只有一个。
异常显示:
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(2131361927, class android.widget.ListView) with Adapter(class com.indus.TAP.PanelDisplay$LogAdaptor)]
08-04 11:42:34.624: E/AndroidRuntime(7133):
代码:
runOnUiThread(new Runnable() {
@Override
public void run() {
LogAdaptor log = (LogAdaptor) lvLog.getAdapter();
log.notifyDataSetChanged();
lvLog.post(new Runnable() {
@Override
public void run() {
lvLog.smoothScrollToPosition(lvLog.getCount());
}
});
}
});
【问题讨论】:
-
你打电话给
notifyDataSetChanged()了吗? -
是的,我在“runOnUiThread”内部调用了
-
@FarooqArshed 此代码将作为信号器回调方法的结果每秒执行一次。
-
您应该发布
lvLog代码。这看起来像是一个时间问题,notifyDataSetChanged()在更改数据的post调用之前被调用。为了验证这个假设,您可以将notifyDataSetChanged()移动到smoothScrollToPosition之后并进行测试 -
@rangarajb2005,请查看stackoverflow.com/questions/30846096/…
标签: android