调用Retrofit后,Android progressBar不会设置进度

时间:2017-06-08 16:49:41

标签: java android progress-bar retrofit progressdialog

我试图模拟改装呼叫的进度,所以在执行呼叫之前我以这种方式开始进度:

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();

}

CustomDialogLoad是一个带有进度和TextView的对话框。

因此,当呼叫结束时,我想将进度设置为100,但progressBar不会改变,我试图以手动方式进行任何更改,但progressBar不想更新。< / p>

以下是改装电话的代码

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)}
    }

我试图根据这个问题取得实际进展

Android Retrofit - onProgressUpdate for showing Progress Notification

但使用multiPart

对我无效

我感谢任何帮助

问候

编辑:我添加了“帖子”来更新进度但仍无法正常工作

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

}

该方法位于CustomDialogLoad类

2 个答案:

答案 0 :(得分:0)

您正在尝试从其他线程更改View。请查看此问题How to set text of text view in another thread

基本上,改变:

 d.setProgress(progress);

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

答案 1 :(得分:0)

像这样更改你的代码

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();

}