0
@
Override
protected Boolean doInBackground(final String...params) {

    runOnUiThread(new Runnable() {
            public void run() {

The problem is when AysncTask executed and onPostExecute() get called (where mAllResultsAdapter.notifyDataSetChanged(); get called), the Adapter has not bound to ListView yet. I get the below exception.

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(2131034153, class android.widget.ListView) with Adapter(class com....tracebuzz.allresults.AllResultsAdapter)]

the problem is the ArrayList that bound to my Adapter is changed in the Background thread.

The content of the adapter has changed but ListView did not receive a notification - With AsyncTask

The above problem is my exact situation, but i am just not sure how to apply in my case where there are such 10 ArrayList getting modified in Back Ground Thread and 10 Adapaters which needs to be notified.

4

4 回答 4

0

我想您应该能够在 AsyncTask 中的 ui 线程上运行代码。

如果您无权访问上下文,请使用

new Handler(Looper.getMainLooper()).post(youRunnable);
于 2013-06-12T15:22:00.150 回答
0

不要在后台线程中更改适配器数据,仅在 UI 线程中。后台线程应该创建一个局部变量列表,并在 onPostExecute 或通过 runOnUiThread 将列表复制到适配器的列表并调用 notifyDataSetChanged

于 2013-06-12T15:20:06.487 回答
0

只需将 notifyDataSetChanged 移动到异步任务的 onPostExecute 方法即可。此方法在 doInBackground 完成后在 UI 线程上运行。这样,您就可以在后台下载和更新适配器内容,并且只需刷新 UI 线程上的实际视图。

于 2013-06-12T15:20:29.843 回答
0

我曾经有同样的问题。我尝试了很多东西,包括 runOnUiThread,但我认为最好的解决方案是在 AsyncTask 中创建一个临时 ArrayList 以将其填充到 doInBackground() 中。然后,您可以将所有这些元素刷新到真正的 ArrayList 中,并在 onPostExecute() 方法(在 UI 线程上运行)中通知适配器。

于 2013-06-12T15:25:54.340 回答