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

看板AndroidDev作者 (揪~~竟,能不能考上?)時間10年前 (2014/05/24 18:56), 編輯推噓7(702)
留言9則, 7人參與, 最新討論串1/1
大家好, 我使用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

05/24 18:58, , 1F
附註:ArrayList在doInBackground()中更新
05/24 18:58, 1F

05/24 19:43, , 2F
印象中,doInBackground()不能做UI操作..
05/24 19:43, 2F

05/24 20:15, , 3F
adapter更新資料後要用notifydatasetchanged
05/24 20:15, 3F

05/24 20:21, , 4F
更新內容要在onpostexecuted做
05/24 20:21, 4F

05/24 21:37, , 5F
你的數據更新是放在哪邊?
05/24 21:37, 5F

05/24 23:12, , 6F
如果是要在執行階段的話要用onProgressUpdate()
05/24 23:12, 6F

05/24 23:31, , 7F
程式碼能完整點嗎?
05/24 23:31, 7F

05/25 09:35, , 8F
二樓正解 Async存在就是避免UIthread和主thread混用
05/25 09:35, 8F

05/25 10:45, , 9F
要在doinbackground作UI更新 請使用publishProgress
05/25 10:45, 9F
文章代碼(AID): #1JW7hObF (AndroidDev)