作者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://webptt.com/cn.aspx?n=bbs/AndroidDev/M.1400928984.A.94F.html
1F:→ HelloJimmy:附注:ArrayList在doInBackground()中更新 05/24 18:58
2F:推 tails32100:印象中,doInBackground()不能做UI操作.. 05/24 19:43
3F:推 jerrysky:adapter更新资料後要用notifydatasetchanged 05/24 20:15
4F:推 jerrysky:更新内容要在onpostexecuted做 05/24 20:21
5F:推 givemepass:你的数据更新是放在哪边? 05/24 21:37
6F:推 tails32100:如果是要在执行阶段的话要用onProgressUpdate() 05/24 23:12
7F:推 slemonade:程式码能完整点吗? 05/24 23:31
8F:推 mshockwave:二楼正解 Async存在就是避免UIthread和主thread混用 05/25 09:35
9F:→ sdyy:要在doinbackground作UI更新 请使用publishProgress 05/25 10:45