Java – the progress dialog box is displayed but no progress bar is displayed and the message is not updated
•
Java
I have a class called from a fragment The progress update is called, but the message is not updated I also don't see any progress bars or spinners Just the title and message, see some similar problems, but no progress bar is not displayed at all In addition, my message will not be updated in onprogressupdate at all, but the printed value does show that it is incremented in onprogressupdate
Editor: This is the way I started the task
DownloadFilesTask download = new DownloadFilesTask(); download.execute(urls.toArray(new String[urls.size()]));
This is the class
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(); }
}
Solution
The problem is how your ProgressDialog is initialized
Replace your onpreexecute with this:
@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(); }
Basically, you need to call setProgressStyle. before calling show.
In addition, you can delete the runonuithread code in onprogressupdate because onprogressupdate. Exe is called in the UI thread
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码