[問題]使用AsyncTask更新List資料的問題

作者: HelloJimmy (Angel Michael)   2014-05-24 18:56:22
大家好,
我使用AsyncTask非同步的去伺服器取得一個ArrayList
讓adapter吃進ArrayList並動態更新ListView裡Item的資料.
點擊Item時會取得Item Position並再去後台取得詳細資訊,
之後用一個Activity呈現這個資訊.
但現在在點擊Item時三不五時就會出現下列Exception:
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.
Google之後我有修正更新通知:
private class getDispatchedTask extends AsyncTask<String, Integer, String>{
...
protected void onPostExecute(String result) {
...
adapter.notifyDataSetChanged();
}
}
但問題還是會發生
請教高手協助
謝謝~
作者: HelloJimmy (Angel Michael)   2014-05-24 18:58:00
附註:ArrayList在doInBackground()中更新
作者: tails32100 (Tails)   2014-05-24 19:43:00
印象中,doInBackground()不能做UI操作..
作者: jerrysky (jerry~sky)   2014-05-24 20:15:00
adapter更新資料後要用notifydatasetchanged更新內容要在onpostexecuted做
作者: givemepass (λ)   2014-05-24 21:37:00
你的數據更新是放在哪邊?
作者: tails32100 (Tails)   2014-05-24 23:12:00
如果是要在執行階段的話要用onProgressUpdate()
作者: slemonade (淳)   2014-05-24 23:31:00
程式碼能完整點嗎?
作者: mshockwave (夏克維夫)   2014-05-25 09:35:00
二樓正解 Async存在就是避免UIthread和主thread混用
作者: sdyy (中壢市的小智)   2014-05-25 10:45:00
要在doinbackground作UI更新 請使用publishProgress

Links booklink

Contact Us: admin [ a t ] ucptt.com