简体   繁体   中英

Android progressBar doesn't set progress after call Retrofit

im trying to simulate a progress of the retrofit's call, so before of execute the call i start the progress in this way:

public void runSimulateProgress(final CustomDialogLoad d){
    new Thread(new Runnable() {
        @Override
        public void run() {
            if (d != null){
                int progress = 0;
                while (d.getProgress() < 99){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    progress = progress+10;
                    d.setProgress(progress);
                }

            }
        }
    }).start();

}

The CustomDialogLoad is a Dialog with a progress and TextView.

So, when the call ends, i want to set progress to 100, but the progressBar doesn't change, i have tried to make any change of progress in a manual way but the progressBar doesn't want to update.

Here's the code of retrofit's call

dialogProgress.runSimulateProgress(dialogProgress);
final SaveRutaVisitaService saveRutaVisitaService =       restAdapter.create(SaveRutaVisitaService.class);
saveRutaVisitaService.saveRutaVisita(multipartTypedOutput, new        Callback<Response>() {
        @Override
        public void success(Response result, Response response) {
       dialogProgress.setProgress(100)

    }   @Override
        public void failure(RetrofitError retrofitError) {
       dialogProgress.setProgress(100)}
    }

I tried to make real progress based on this question

Android Retrofit - onProgressUpdate for showing Progress Notification

but doesn't work to me with multiPart

I appreciate any help

regards

EDIT: i added the "post" to update the progress but still not working

public void setProgress(final int p){
    mRingProgressBar.post(new Runnable() {
        @Override
        public void run() {
            mRingProgressBar.setProgress(p);
        }
    });

}

that method is inside of CustomDialogLoad class

You're attempting to change View from another thread. Check this question How to set text of text view in another thread

Basically, change:

 d.setProgress(progress);

to

d.post(new Runnable() {
    public void run() {
        d.setProgress(progress);
    } 
}

change your code like this

public void runSimulateProgress(final CustomDialogLoad d){
new Thread(new Runnable() {
    @Override
    public void run() {
        if (d != null){
            int progress = 0;
            while (d.getProgress() < 99){
                try {
         ((Activity) ctx).runOnUiThread(new Runnable() {
            public void run() {
                progress = progress+10;
              d.setProgress(progress);
            }
        }
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        }
    }
}).start();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM