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