Reputation: 1
There's a "download" button in each listview item. While the button is clicked, it will start a worker thread to down files. And at the same time, the button changed to progressbar and showing the progress rate.
So please show me some proper ways.
Upvotes: 0
Views: 325
Reputation: 3283
Something like this:
public class DownloadTask extends AsyncTask<Void, Void, Boolean> {
protected void onPreExecute() {
ProgressDialog() progress = new ProgressDialog(context);
progress.setMessage("Loading ...");
progress.show();
}
protected Boolean doInBackground(Void... arg0) {
// Do work
return true;
}
protected void onPostExecute(Boolean result) {
progress.dismiss();
}
}
This should be nested in your activity class and executed like this:
new DownloadTask().execute();
You will likely need to adjust the asynctask to fit your needs but this will get you started.
Upvotes: 1
Reputation: 7915
Use an AsyncTask since it has special methods for communicating with the main (UI) thread despite being asynchronous.
Here is an example: http://android-er.blogspot.com/2010/11/progressbar-running-in-asynctask.html
Upvotes: 1