36

我只是尝试实现一个进度对话框,在我漫长而复杂的计算过程中,我遇到了一些更改文本的问题。

for (String aString:myStringArray){
    Log.v(TAG, aString);
    mProgressDialog.incrementProgressBy(1);
    mProgressDialog.setMessage(aString);
}

我可以清楚地看到 incrementProgressBy 正在工作并且我的对话框正在更新,但消息没有改变。

关于如何使它工作的任何想法?

非常感谢。

4

2 回答 2

59

刚刚找到答案,效果很好:

runOnUiThread(changeMessage);

使用该代码:

private Runnable changeMessage = new Runnable() {
    @Override
    public void run() {
        //Log.v(TAG, strCharacters);
        m_ProgressDialog.setMessage(strCharacters);
    }
};
于 2010-10-16T01:54:12.087 回答
0

Firebase我循环上传图片并更新ProgressDialog每个图像:

(我在一个Fragment,所以我getActivity()在调用之前使用runOnUiThread()

List<Bitmap> bitmaps;
int picCounter = 1;
...

progressDialog = ProgressDialog.show
 (getContext(), "sending...", "just a minute", false, false);

new Thread(new Runnable() {
    @Override
    public void run() {
        for (int i = 0; i < bitmaps.size(); i++) {

            String filename = String.valueOf(i);

            uploadPic(bitmaps.get(i), "img" + filename, new MyCallback() {
                @Override
                public void onFinish() {
                    picCounter++;
                    Objects.requireNonNull(getActivity()).runOnUiThread(new Runnable() {
                        public void run() {
                            progressDialog.setTitle ("upoading " + picCounter + "image from " + bitmaps.size());
                        }
                    });
                }
            });
        }
   }
}).start();

上传图片方法:

public interface MyCallback { void onFinish ();}

private void uploadPic(final Bitmap bitmap, final String fileName, final MyCallback callback) {
   ... // uploading to firebase and then:
   callback.onFinish();
}
于 2021-02-03T11:03:34.317 回答