0

在我的AmazingListView中选择项目时出现以下错误。我通过AsyncTask(inBackround())加载数据,每调用20个项目并调用adapter.notifyDataSetChanged()方法onPostExecute。该列表加载时没有任何问题,当我在加载列表后选择一个项目时,我没有任何问题......我只在列表加载期间选择项目时出现此错误。在AmzingListView here中打开了一个问题,但这两个解决方案都没有帮助我。任何想法都会被处理。使用AsyncTask加载AmazingListView时出错

错误:

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(2131034170, class com.foound.widget.AmazingListView) with Adapter(class android.widget.HeaderViewListAdapter)] 

代码:

private ArrayList<object> list; 


private class AsyncTask extends AsyncTask<Void, object, Void> 
{ 

    @Override 
    protected void onPreExecute() 
    { 

        list = populatList(); 

     super.onPreExecute(); 
    } 

    @Override 
    protected Void doInBackground(Void... urls) 
    { 
        Log.i("CallLogActivity", "CallLog is Loading"); 
     for (int i = 0; i < list.size(); i++) 
       { 

        String str = getString(); 
        boolean bool = getBoolean(); 
        Object c = new Object(str,bool); 
        publishProgress(c); 
       } 

     //Doing Work Here!! 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) 
    { 
      adapter.notifyDataSetChanged(); 
    } 

    @Override 
    protected void onProgressUpdate(object... object) 
    {   
     count++ 
        updatelist(object); 
     if (adapter != null) 
     { 
      if (count > 20) 
      { 
       adapter.notifyDataSetChanged();   
       count = 0; 
      } 
     } 
     super.onProgressUpdate(object); 
    } 
} 
+0

你是否在你的“做工”代码中更改适配器的内容? (因为我注意到你的postExecute是'Void') – Tom

+0

是的我在“做功”期间改变了适配器的内容,我发现它一直在更新。问题是当我在列表中选择项目时。 – user1163234

+0

您是否检查过错误消息是否始终没有说出真相 - 作为第一步,将代码中将适配器更改为runOnUIThread(Runnable ...)的部分。 (http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)) – Tom

回答

1

原来我的问题是调用adapter.notifyDataSetChanged();只有20次更改后。我需要adapter.notifyDataSetChanged();每改变一次...发现它here

+0

我很高兴你解决了你的问题。但是你看,你*是*改变了adpater的内容!这个错误信息并不在这里。 – Tom