我正在嘗試更新我的列表並在我的活動中顯示更新的列表。每次我這樣做,我得到以下錯誤Android如何正確通知數據集更改爲ListView
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.
這是我的代碼,我使用AsyncTask來實現此任務。我在我的適配器上調用了notifyDataSetChanged(),但它似乎不起作用。我究竟做錯了什麼?
class MyTask extends AsyncTask<Context, Integer, String> {
@Override
protected String doInBackground(Context... params) {
GregorianCalendar currentDateclone = (GregorianCalendar) currentDate.clone();
ListPopulater2.listPopulate(currentDateclone, 7, items, Id);
//method to update list info
}
// -- gets called just before thread begins
@Override
protected void onPreExecute() {
progressdialog = ProgressDialog.show(SectionListExampleActivity.this, "", "Loading..."); //display progress bar
super.onPreExecute();
}
// -- called as soon as doInBackground method completes
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.d("postExecute", "here");
adapter.notifyDataSetChanged();
progressdialog.cancel();
}
}
與你的問題沒有關係,但你爲什麼在'onPostExecute(...)'中使用'runOnUiThread(...)'?無論如何,'AsyncTask'的所有方法(除了'doInBackground(...)')都在UI線程上運行。 – Squonk 2012-07-11 00:46:41
我剛剛嘗試的東西,沒有它的工作原理。我只是刪除它清晰 – 2012-07-11 00:49:45