views:

268

answers:

1

Hi, I'm create a download manager with listview and it will display the current downloaded by progressbar. May I know how can i update the progressbar?

for example. i have 3 class.

"main.class" = is a listview.

"mAdapter.class" = is extends ArrayAdapter.

"mThread.class" = implements Runnable to start the download.

I have google, and found to update the listview, or redrawing the UI should use runOnUIThread. but how can i combine those 3 class together? I have try

mThread.start(){
   main.runOnUIThread(updated);
}
private updated = new Runnable(){
   mAdapter.progressbar.setProgress(downloaded);
}

this 2 method i was put in "mThread.class". but it doesn't work. May I know what i was my misstake?

P/S:I'm sorry about my ugly code and bad english. Hope you guys understand what I'm talking about. Thanks

+1  A: 

You need to use the AsyncTask. See the docs for description and examples. http://developer.android.com/intl/zh-TW/reference/android/os/AsyncTask.html

Konstantin Burov
Hi Konstantin, thanks for reply. I saw this method before. but I no really know how to use it. for example that 3 parameters AsyncTask<"param1", "param2", "param3">. what should I put on "param1" and "param2"? a thread for the downloading? "param3" is it put the result what I'm getting? and how to used that 4 steps? I'm sorry about my english and the stupid question. your reply is appreciated.
WynixToo
Hi here is better guide about AsyncTask. It contains pretty good code example with comments. http://www.screaming-penguin.com/node/7746
Konstantin Burov
Hi, sorry for late reply. I have try it but my senior said for downloading should not use AsyncTask. it use use handle message and runOnUIThread. is it true?
WynixToo
http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html here is info about AsyncTask from officials. They say it's best thing to deal with multithreading in Android. I'm not sure how AsyncTask is implemented exactly, it may be true what you say, but I'm sure that it's hardly possible to write code better then it is already done by platform developers.
Konstantin Burov
Also by not using standard API you leave yourself without possible future optimizations from platform developres.
Konstantin Burov