我想用裏面的進度條創建一個對話框。 但我看到Toas消息,但我沒有看到對話框和tre進度條..爲什麼?progressDialog和AsyncTask
thx很多evherone。
//prepare for a progress bar dialog
progressBar = new ProgressDialog(this);
progressBar.setCancelable(true);
progressBar.setMessage("Creazione Database\nIngredienti...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.show();
new BackgroundAsyncTask(appContext, progressBar).execute();
... ... ...
,我已經創建了一個通用類:
public class BackgroundAsyncTask extends AsyncTask<Void, Integer, Void> {
int myProgressCount;
Context context;
ProgressDialog progressBar;
public BackgroundAsyncTask(Context appContext, ProgressDialog progressBar) {
// TODO Auto-generated constructor stub
this.context = appContext;
this.progressBar = progressBar;
}
@Override
protected void onPreExecute() {
Toast.makeText(context, "onPreExecute Start Progress Bar", Toast.LENGTH_LONG).show();
progressBar.setProgress(0);
myProgressCount = 0;
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
while (myProgressCount < 100) {
myProgressCount++;
/**
* Runs on the UI thread after publishProgress(Progress...) is
* invoked. The specified values are the values passed to
* publishProgress(Progress...).
*
* Parameters values The values indicating progress.
*/
publishProgress(myProgressCount);
SystemClock.sleep(100);
}
return null;
}
/**
* This method can be invoked from doInBackground(Params...) to publish
* updates on the UI thread while the background computation is still
* running. Each call to this method will trigger the execution of
* onProgressUpdate(Progress...) on the UI thread.
*
* onProgressUpdate(Progress...) will not be called if the task has been
* canceled.
*
* Parameters values The progress values to update the UI with.
*/
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
progressBar.setProgress(values[0]);
}
@Override
protected void onPostExecute(Void result) {
Toast.makeText(context, "onPostExecute End Progress Bar",Toast.LENGTH_LONG).show();
progressBar.dismiss();
}
}
你真的叫'.show() '在'ProgressBar'上? – nKn