我嘗試使用列表視圖更新一個異步調用完成後的片段,但我得到以下錯誤: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:我使用的通用圖像裝載機填充列表上的圖像。
檢查此 - 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
請發佈您的'doInBackground()'方法 – earthw0rmjim
可能重複的[Android,ListView IllegalStateException:「適配器的內容已更改,但ListView沒有收到通知」](http://stackoverflow.com/questions/3132021/ android-listview-illegalstateexception-the-the-adapter-has-changed) – NarendraJi