I am trying to think of a way to have Progress Bar or any other working Dialog with loading bar in my code, but I can't find any good code example, they are all too simplistic and don't give you an idea of how to use it when your code is more complicated. For example, here is the code from Android docs:
public class MyActivity extends Activity {
private static final int PROGRESS = 0x1;
private ProgressBar mProgress;
private int mProgressStatus = 0;
private Handler mHandler = new Handler();
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.progressbar_activity);
mProgress = (ProgressBar) findViewById(R.id.progress_bar);
// Start lengthy operation in a background thread
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();
}
}
and here is how my application works: When a button is pressed it starts an async task, like this
public void findProduct(View v) {
operationID = 1;
JSONGetProduct getp = new JSONGetProduct(this);
getp.execute();
}
}
then this asynctask calls back with delegate and in this method, onFinishedParsing I have a switch that in each case it starts another asynctask
@Override
public void onFinishedParsing(ArrayList<?> objects) {
switch (operationID) {
case 1:
operationID = 2;
JSONGetPrice getprice = new JSONGetPrice(this);
getprice.execute();
break;
case 2:
operationID = 3;
JSONGetShop gets = new JSONGetShop(this);
gets.execute();
break;
case 3:
// starts another activity
break;
what I need is to have the progress bar initialized in the button press and in each of this cases I can set it's progress and then in the final case to remove the progress bar.... How can I do that?