0
我在考虑如何为一个Activity打开一个布局,其内容将基于下载的解析的json数据。在Android中使用带有下载数据的ListAdapter
我认为使用AsyncTask的1)下载数据,2)解析它和3)更新UI但我使用的ListAdapter必须在主线程,所以我不能把它放在onPostExecute方法的的AsyncTask。我有点困惑,应该采取什么方法。任何指针非常赞赏。
我在考虑如何为一个Activity打开一个布局,其内容将基于下载的解析的json数据。在Android中使用带有下载数据的ListAdapter
我认为使用AsyncTask的1)下载数据,2)解析它和3)更新UI但我使用的ListAdapter必须在主线程,所以我不能把它放在onPostExecute方法的的AsyncTask。我有点困惑,应该采取什么方法。任何指针非常赞赏。
做到像本例:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i/(float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
http://developer.android.com/reference/android/os/AsyncTask.html
你在doInBackground(参数...段)什么都想要。如果你想更新UI或你的ListAdapter。请致电publishProgress(Params pa),它将调用onProgressUpdate(Integer .. progress)。在这个函数中,你可以更新UI,更新数据或在主线程上做你想做的事情。
希望它有帮助。
感谢您的思考。我理解这一点,但是你会在onProgressUpdate或onPostExecute中使用哪些方法来更新(或者更好,首次膨胀)ListAdapter? – Egis 2013-03-09 13:33:03
如果您想要始终更新UI,请使用onprogressupdate执行此操作。否则,如果只想解析一次数据,则使用onepostexecute。因为,当doinbackground完成后,它再次调用onpostexecute – loose11 2013-03-09 13:52:37
,我明白这一点:)但回到我的问题,你会在onProgressUpdate和onPostExecute中调用什么方法来实现我的目标?谢谢 – Egis 2013-03-09 15:31:10