如何更新从AsyncTask的UI [英] How to update ui from asynctask
问题描述
我已经看到了如何做到这一点的例子很多,但我可以计算如何在我的code实现它。
I've seen many examples of how to do this, but I can figure how to implement it in my code.
我用这个 code 。
我已经更新了网址,因此它会收到与动态数据的JSON。
我想要做的是自动与此code更新列表,每30秒。
I using this code.
I have updated the url, so it will receive a json with dynamic data.
What I'm trying to do is to automatically update the list every 30 secs with this code.
Handler handler = new Handler();
Runnable refresh = new Runnable() {
public void run() {
new GetContacts().execute();
handler.postDelayed(refresh, 30000);
}
};
据刷新,并调用URL并获取数据,但用户界面不会得到更新。
It refreshed, and call the url and gets the data, but the UI does not get updated.
感谢任何提示,帮助我在正确的方向。
Thank for any hints that helps my in the right direction.
<一个href="http://www.androidhive.info/2012/01/android-json-parsing-tutorial/">http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
推荐答案
您有可以与UI交互的AsyncTask的三个保护的方法。
You have three protected methods in an AsyncTask that can interact with the UI.
-
在preExecute()
- 在运行的在的
doInBackground()
onPreExecute()
- runs before
doInBackground()
- 在运行的在的
doInBackground()
完成
- 在此仅运行时
doInBackground()
与称之为publishProgress()
如果你的案件的任务运行了很多的时间比你要刷新你想利用
onProgressUpdate的()
和<$ C $的30秒C> publishProgress()。否则onPostExecute()
应该做的伎俩。If in your case the Task runs for a lot longer than the 30 seconds you want to refresh you would want to make use of
onProgressUpdate()
andpublishProgress()
. OtherwiseonPostExecute()
should do the trick.请参阅如何实现它的官方文档。
这篇关于如何更新从AsyncTask的UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- runs before
- 在运行的在的