後,我有我的適配器列表視圖我一個問題,我不知道我必須在我的代碼添加notifyDataSetChanged()始終如果我運行logcat的這段代碼是未來一個錯誤:Android的ListView控件更新的AsyncTask
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. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131165276, class android.widget.ListView)]
我的AsyncTask:
class UpdateData extends AsyncTask<Void, Void, JSONArray> {
String result = "";
JSONArray jArray = null;
@Override
protected void onPostExecute(JSONArray result) {
super.onPostExecute(result);
adapter.addAll(deptList);
adapter.notifyDataSetChanged();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected JSONArray doInBackground(Void... arg0) {
....
try {
JSONArray array = new JSONArray(result);
for (int i = 0; i < array.length(); i++) {
JSONObject j = array.getJSONObject(i);
EventCalendarStrings d = new EventCalendarStrings();
d.name = j.optString("name", "");
d.kategorie = j.optString("Art", "");
deptList.add(d);
}
} catch (JSONException e) {
Log.e("log_tag", "No connection " + e.toString());
}
}
return jArray;
}
確保deptList(最好使用當地的UpdateData級存儲)只在一個線程中使用。因爲它是外部變量,我懷疑它用在適配器中,因爲它的數據和...也許我錯了,但我沒有看到代碼。 – Deucalion 2014-10-08 19:50:32
'deptList'是適配器的數據源嗎? – dharms 2014-10-09 14:52:43