IllegalStateException:适配器的内容已更改,但ListView未收到通知

时间:2015-02-28 13:25:24

标签: android listview illegalstateexception

我知道,当它抛出IllegalStateException时,直截了当。但就我而言,我不能猜测,源头的麻烦在哪里。我敢说,麻烦就行了:

private class LoadMoreDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        if (isCancelled()) {
            return null;
        }
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
        }
        OFFSET = OFFSET + COUNT;
        getWallsData();
        return null;
    }
    @Override
    protected void onPostExecute(Void result) { 

        mPostListAdapter.notifyDataSetChanged();                
        wallPostsList.onLoadMoreComplete();
        super.onPostExecute(result);
    }
    @Override
    protected void onCancelled() {
        wallPostsList.onLoadMoreComplete();
    }

有一条logcat消息:

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(2131099757, class com.costum.android.widget.LoadMoreListView) with Adapter(class android.widget.HeaderViewListAdapter)]

1 个答案:

答案 0 :(得分:0)

问题得到解决:答案被发现 there ! 我做了什么?我添加了切换器,布尔LOADMORE SWITCHER = false;作为全局可见性并替换为mPostListAdapter.notifyDataSetChanged();从doInBackground()到getWallsData()。

private Boolean LOADMORE SWITCHER=false;
....
private class LoadMoreDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        if (isCancelled()) {
            return null;
        }
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
        }
        OFFSET = OFFSET + COUNT;
        LOADMORE=true;
        getWallsData();
        return null;
    }
    @Override
    protected void onPostExecute(Void result) { 

       // mPostListAdapter.notifyDataSetChanged(); was replaced to getWallsData() body
        wallPostsList.onLoadMoreComplete();
        super.onPostExecute(result);
    }
    @Override
    protected void onCancelled() {
        wallPostsList.onLoadMoreComplete();
    } 
}
.....
getWallsData(){
     ...
     if(LOADMORE){
          mPostListAdapter.notifyDataSetChanged();
          LOADMORE=false;


     }
}

我非常高兴地说:我不再有这个问题:

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(2131099757, class com.costum.android.widget.LoadMoreListView) with Adapter(class android.widget.HeaderViewListAdapter)]