在递增时更新ProgressBar消息

时间:2014-02-13 05:54:29

标签: android progress-bar progressdialog android-progressbar

我有进度条,通过定义asyncTask来显示下载文件的进度以完成此操作。现在我想显示下载状态,因为进度条已更新。例如。 “当前文件:40%”,在下一次迭代中,它显示“当前文件:80%”等

以下是我的代码:

 public class MainActivity extends Activity {

 private ProgressDialog _progressDialog;
 private int _progress = 0;

 @Override
 protected 
 void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Button btn = (Button) findViewById(R.id.button1);
  btn.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    final DownloadTask downloadTask = new DownloadTask(MainActivity.this);
    downloadTask.execute("xyz.com");
   }
    });
  }

 protected
 Dialog onCreateDialog(int id) {
  switch (id) {
  case 0:
   //...
  case 1:
   _progressDialog = new ProgressDialog(this);
  _progressDialog.setIcon(R.drawable.ic_launcher);
  _progressDialog.setTitle("Downloading files...");
  _progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

  _progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Hide",new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog,int whichButton) {
    Toast.makeText(getBaseContext(), "Hide clicked!",Toast.LENGTH_SHORT).show();
   }
  });
  _progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"Cancel", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog,int whichButton) {
    Toast.makeText(getBaseContext(), "Cancel clicked!",Toast.LENGTH_SHORT).show();
   }
  });
  return _progressDialog;
  }
  return null;
 }
 private class DownloadTask extends AsyncTask<String, Integer, String> {
     private Context context;
     int myProgressCount;
     public DownloadTask(Context context) {
         this.context = context;
     }
     @Override
     protected String doInBackground(String... sUrl) {            
             try {
                 while (......) {
                   if (isCancelled())
                          return null;

         myProgressCount++;                    

         publishProgress(myProgressCount);    

        }                

             } catch (Exception e) {
                 return e.toString();
             }         
         return null;
     }


     @Override
     protected void onPreExecute() {
         super.onPreExecute();     
         showDialog(1);   
   _progress = 0;
   _progressDialog.setProgress(0);   
     }
     @Override
     protected void onProgressUpdate(Integer... progress) {
         super.onProgressUpdate(progress);
         _progressDialog.setIndeterminate(false);
         _progressDialog.setMax(100);
         _progressDialog.setProgress(progress[0]);
         _progressDialog.setMessage("File:"+progress[0]);
     }
     @Override
     protected void onPostExecute(String result) {
      _progressDialog.dismiss();
         if (result != null)
             Toast.makeText(context,"Download error: "+result, Toast.LENGTH_LONG).show();
         else
             Toast.makeText(context,"File downloaded", Toast.LENGTH_SHORT).show();         
          }    
         }
     }

如何在进度[0]更改时更新setMessage?

由于

0 个答案:

没有答案