LorenzoBerti
LorenzoBerti

Reputation: 6974

How show ProgressBar update with Volley Android

I have a problem with showing progress on ProgressBar.

I have function that calls my server with Volley, when return result I have implemented a Callback that return data to my activity.

Something like:

public static void saveData(Data data, final VolleyCallbackJsonObject callback){
  ...
  callback.onSuccessResponse(new JSONObject());
  ...
}

Supposing that the function is called on cycle like:

for(int i = 1; i<=5; i++){
   final int copyIndex = i;
   MyClass.saveData(new Data(i), new VolleyCallbackJsonObject() {
      @Override
         public void onSuccessResponse(JSONObject result) {
           callFunctionForShowAndUpdateProgress(copyIndex);
         }
      })
  }
}

I would like show a ProgressBar that show a bar with a progress from 1/5 to 5/5

so I have my Function in my activityClass:

public Class Test extends Activity{
  private ProgressBar pb;
  public void callFunctionForShowAndUpdateProgress(int index){
     if(index == 1){
        pb = (ProgressBar) findViewById(R.id.pbLoading);
        pb.setMax(5);
        pb.setIndeterminate(false);
        pb.setVisibility(View.VISIBLE);
     }
     if(pb != null){
       pb.setProgress(index);
     }
   if(index == 5){
     pb.postDelayed(new Runnable() {
        @Override
        public void run() {
           pb.setVisibility(View.GONE);
        }
     }, 3000);
   }
  }
}

My problem is that progress bar not set progress, but it show only at the end so with 5/5 and not show the progress step (1/5, 2/5, 3/5, 4/5) and then it will hide after 3 seconds how specified on postDelayed.

So How can I show all the step? what is wrong?

Upvotes: 1

Views: 1578

Answers (2)

Mohammad Elsayed
Mohammad Elsayed

Reputation: 2066

Try to postDelay the method 'callFunctionForShowAndUpdateProgress' in the method 'onSuccessResponse' instead the postDelay you are already making because the delay you are making happens only when i = 5, so technically the progress goes through 2, 3, and 4 but you don't see it.

try this inside 'onSuccessResponse':

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
                  callFunctionForShowAndUpdateProgress(copyIndex);
    }
 }, 3000);



By the way, When the method 'callFunctionForShowAndUpdateProgress' is called in the 'onSuccessResponse' the index initially will be 1 and that is fine, the compiler then will go to second if statement

if(pb != null){
   pb.setProgress(index);
}

and will execute it because the condition is correct there, it will not cause a problem but leaving it like this is not a good practice, to solve this prevent the compiler from getting into this if statement by adding else.

I hope that helps

Upvotes: 1

Vikash Patel
Vikash Patel

Reputation: 36

Change your cycle calling function as like below and then check,

int copyIndex = 1;
callFunctionForShowAndUpdateProgress(copyIndex);
for(int i = 1; i<=5; i++)
{
     MyClass.saveData(new Data(i), new VolleyCallbackJsonObject() 
     {
        @Override
        public void onSuccessResponse(JSONObject result) 
        {
          copyIndex++;
          callFunctionForShowAndUpdateProgress(copyIndex);
        }
     })
}

Upvotes: 0

Related Questions