java – 进度对话框显示但没有进度条显示和消息未更新

前端之家收集整理的这篇文章主要介绍了java – 进度对话框显示但没有进度条显示和消息未更新前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个从片段调用的类.在进度更新被调用但消息不会更新.我也没有看到任何进度条或微调器.只是标题和消息,看到一些类似的问题,但没有任何进度条根本没有显示.此外,我的消息根本不会在onProgressUpdate中更新,但打印出的值确实显示它在onProgressUpdate内增量.

编辑:这是我开始任务的方式

DownloadFilesTask download = new DownloadFilesTask();
download.execute(urls.toArray(new String[urls.size()]));

这是班级

private class DownloadFilesTask extends AsyncTask<String,Integer,Long> {

ProgressDialog progressDialog;

@Override
protected void onPreExecute()
{
    progressDialog = ProgressDialog.show(getActivity(),"Downloading","Downloaded 0/"+urls.size(),false);
    progressDialog.setProgress(0);
    progressDialog.setMax(urls.size());
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
}

@Override
protected Long doInBackground(String[] urls) {
    int count = urls.length;
    long totalSize = 0;
    for (int i = 0; i < count; i++) {
            //Do things in background here
            publishProgress(new Integer[] {i});
    }
    return totalSize;
}

@Override
protected void onProgressUpdate(final Integer... progress) {
    System.out.println(progress[0]); //This does print correctly
    getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            progressDialog.setProgress(progress[0]);
            progressDialog.setMessage("Downloaded "+ progress[0] +"/"+urls.size());
        }
    });
}

@Override
protected void onPostExecute(Long result) {
    progressDialog.dismiss();
    Toast t = Toast.makeText(getActivity(),"Downloaded",Toast.LENGTH_LONG);
    t.show();
}

}

解决方法

问题在于您的ProgressDialog初始化方式.

用这个替换你的onPreExecute:

@Override
protected void onPreExecute() {
    progressDialog = new ProgressDialog(activity);
    progressDialog.setTitle("Downloading");
    progressDialog.setMessage("Downloaded 0/" + urls.size());
    progressDialog.setIndeterminate(false);
    progressDialog.setProgress(0);
    progressDialog.setMax(urls.size());
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressDialog.show();
}

基本上,您需要在调用show之前调用setProgressStyle.

此外,您可以删除onProgressUpdate中的runOnUiThread代码,因为在UI线程上调用onProgressUpdate.

原文链接:https://www.f2er.com/java/128514.html

猜你在找的Java相关文章