Reputation: 487
I have a search function (using a web service) which takes a while to load data to my inflated layout. I want to show a ProgressBar
untill the data is fetched from the web service. This is a small time interval and hence I don't to use a ProgressDialog
for this. I've gone through many posts and dev guide but I'm not able to figure out a way to use that in my app. I've a Spinner
in my layout. Once I choose an item from the spinner drop down I pass the string to the search function which fetches as well as loads data into the inflated layout.
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Selection = spinner.getSelectedItem().toString();
search(Selection);
}
I created a ProgressBar, handler as explained in the dev guide.
private ProgressBar mProgress = new ProgressBar(SearchActivity.this, null, android.R.attr.progressBarStyleSmall);
private int mProgressStatus = 0;
private Handler mHandler = new Handler();
And say this is a new thread used to display the ProgressBar
new Thread(new Runnable() {
public void run() {
while (mProgressStatus < 100) {
mProgressStatus = doWork();
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
mProgress.setProgress(mProgressStatus);
}
});
}
}
}).start();
How do I track progress of my search function to show the ProgressBar
?
Thank you for reading
Upvotes: 2
Views: 2516
Reputation: 26547
Use an AsyncTask
instead of a thread. It's way easier to use and provides a built-in mechanism to be notified of progress updates.
Look at this question for a complete sample :
Download a file with Android, and showing the progress in a ProgressDialog
Upvotes: 1