2014-04-27 98 views
0

我想用裏面的進度條創建一個對話框。 但我看到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(); 
    } 
} 
+0

你真的叫'.show() '在'ProgressBar'上? – nKn

回答

0

The progress range is 0..10000. - 爲docs說。使用適合該範圍的數字。

0

我強烈建議改用對話框

公共類ProgressDialogFragment對話片段延伸DialogFragment {

public static LoadingDialogFragment newInstance() { 
    ProgressDialogFragment frag = new ProgressDialogFragment(); 
    return frag; 
} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

    final ProgressDialog dialog = new ProgressDialog(getActivity()); 
    dialog.setMessage(getString(R.string.loading_text)); 
    dialog.setIndeterminate(true); 
    dialog.setCancelable(false); 

    // Disable the back button 
    OnKeyListener keyListener = new OnKeyListener() { 

     @Override 
     public boolean onKey(DialogInterface dialog, int keyCode, 
       KeyEvent event) { 

      if(keyCode == KeyEvent.KEYCODE_BACK){     
       return true; 
      } 
      return false; 
     } 


    }; 
    dialog.setOnKeyListener(keyListener); 
    return dialog; 
} 

}

更多的信息在這裏https://gist.github.com/dhawalhshah/1355547