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

时间:2015-11-03 01:12:34

标签: java android progressdialog

我有一个从片段调用的类。在进度更新被调用但消息不会更新。我也没有看到任何进度条或微调器。只是标题和消息,看到一些类似的问题,但没有任何进度条根本没有显示。此外,我的消息根本不会在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();
}

}

2 个答案:

答案 0 :(得分:4)

问题在于您的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

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

答案 1 :(得分:0)

不确定确切原因,但setProgress上的runnable不需要onProgressUpdate因为getActivity().runOnUiThread(new Runnable() { @Override public void run() { progressDialog.setProgress(progress[0]); progressDialog.setMessage("Downloaded "+ progress[0] +"/"+urls.size()); } });将在UI线程上运行。

尝试替换

progressDialog.setProgress(progress[0]); progressDialog.setMessage("Downloaded "+ progress[0] +"/"+urls.size());

doInbackground()

此外 - 您是否正在onPostExecute()中进行网络工作,否则我们无法看到Headers上将被解雇的进展。