0

我嘗試使用列表視圖更新一個異步調用完成後的片段,但我得到以下錯誤:java.lang.IllegalStateException:適配器的內容已更改,但ListView未收到通知 - 使用通用映像加載程序的列表

java.lang.IllegalStateException:適配器的內容已更改,但ListView未收到通知。

我正在更新適配器onPostExecute,並且還檢查了該函數正在主線程上運行,但仍然收到IllegalStateException。

@Override 
    protected void onPostExecute(List<String> result) { 
     if (null != mClient) 
      mClient.close(); 
     //TODO 
      //Constants.IMAGES = result.toArray(new String[result.size()]); 
     Constants.IMAGES.addAll(result); 
      Log.i("IMAGES", IMAGES.toString()); 
      if (mTheListener != null && firstCallFlag) { 
       firstCallFlag = false; 
       mTheListener.GotoNextScreen(); 
      }else{ 
       dataUpdateListener.dataUpdated(result); 
      } 
    } 

@Override 
    protected List<String> doInBackground(Void... params) { 

     if(afterTag != null){ 
      URL += "?after="+afterTag; 
      System.out.println("::New URL:: "+ URL); 
     } 
     HttpGet request = new HttpGet(URL); 
     JSONResponseHandler responseHandler = new JSONResponseHandler(); 
     try { 
      return mClient.execute(request, responseHandler); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 


@Override 
public void dataUpdated(List<String> data) { 
    // TODO Auto-generated method stub 
    System.out.println("::Notify data update listener called"); 
    System.out.println("Looper "+Looper.myLooper()); 
    System.out.println("Main Looper "+Looper.getMainLooper()); 
    imageUrls.addAll(data); 
    mAdapter.notifyDataSetChanged(); 
} 

//片段onCreateView - 適配器在這裏設置

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fr_image_list, container, 
      false); 
    listView = (ListView) rootView.findViewById(android.R.id.list); 

    // Adding the load more 

    // LoadMore button 
    Button btnLoadMore = new Button(getActivity().getApplicationContext()); 
    btnLoadMore.setText("Load More"); 

    // Adding Load More button to lisview at bottom 
    listView.addFooterView(btnLoadMore); 

    /** 
    * Listening to Load More button click event 
    * */ 
    btnLoadMore.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // Starting a new async task 
      Constants.getUrlFromRedditService(); 
     } 
    }); 
    mAdapter = new ImageAdapter(); 
    ((ListView) listView).setAdapter(new ImageAdapter()); 

    listView.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      startImagePagerActivity(position); 
     } 
    }); 
    return rootView; 
} 

P.S:我使用的通用圖像裝載機填充列表上的圖像。

+0

檢查此 - http://www.compdigitec.com/labs/2013/11/16/fixing-illegalstateexception-the-content-of-the-adapter-has-changed-but-listview-did-not -receive-a-notification/ – NarendraJi

+0

請發佈您的'doInBackground()'方法 – earthw0rmjim

+0

可能重複的[Android,ListView IllegalStateException:「適配器的內容已更改,但ListView沒有收到通知」](http://stackoverflow.com/questions/3132021/ android-listview-illegalstateexception-the-the-adapter-has-changed) – NarendraJi

回答

2

嘗試

((ListView) listView).setAdapter(mAdapter); 

更換的代碼下面的行

((ListView) listView).setAdapter(new ImageAdapter()); 

看到,ListView控件你總是設置一個新的適配器,並在您onPostExecute所通知到的不同實例適配器

+0

謝謝!有時候只需要一雙額外的眼睛。 – May13ank

+0

是....接受答案,如果有幫助 – nvinayshetty

0

我想這是有些問題,在您的適配器getView()功能,當實例convertView,你應該提供參數的內容,你最好使用 ActivityName.this,而不是getApplicationContext();

+0

它與任何'Context'無關。數據沒有在引起問題的'UI'線程中得到更新。 – Piyush

相關問題