作者HelloJimmy (揪~~竟,能不能考上?)
看板AndroidDev
標題[問題]使用AsyncTask更新List資料的問題
時間Sat May 24 18:56:22 2014
大家好,
我使用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();
}
}
但問題還是會發生
請教高手協助
謝謝~
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.168.106.64
※ 文章網址: http://www.ptt.cc/bbs/AndroidDev/M.1400928984.A.94F.html
→ HelloJimmy:附註:ArrayList在doInBackground()中更新 05/24 18:58
推 tails32100:印象中,doInBackground()不能做UI操作.. 05/24 19:43
推 jerrysky:adapter更新資料後要用notifydatasetchanged 05/24 20:15
推 jerrysky:更新內容要在onpostexecuted做 05/24 20:21
推 givemepass:你的數據更新是放在哪邊? 05/24 21:37
推 tails32100:如果是要在執行階段的話要用onProgressUpdate() 05/24 23:12
推 slemonade:程式碼能完整點嗎? 05/24 23:31
推 mshockwave:二樓正解 Async存在就是避免UIthread和主thread混用 05/25 09:35
→ sdyy:要在doinbackground作UI更新 請使用publishProgress 05/25 10:45