2015-10-23 108 views
-1

我有一個用於存儲複製文件的功能。當我點擊複製Button時,將執行方法複製操作。下面這個方法:無法在onClick上顯示對話框

public void actionCopy() { 
     Dialog dialog = new Dialog(this); 
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialog.setContentView(R.layout.progress_layout); 

     TextView progressTitle = (TextView) dialog.findViewById(R.id.progress_title); 
     progressTitle.setText("Copying..."); 
     final ProgressBar progressBar = (ProgressBar) dialog.findViewById(R.id.progress_bar); 
     progressBar.setMax(listData.size()); 
     progressBar.setProgress(0); 

     dialog.show(); 

     for(int i = 0; i < listData.size(); i++) { 
      String value = new CopyDataUtils(listData.get(i)).execute().get(); 
      progressBar.setProgress(i + 1); 
      Log.i(TAG, value); 
    } 
} 

而且CopyDataUtils的AsyncTask:

public class CopyDataUtils extends AsyncTask<Void, Void, String> { 

    private DataItem item; 

    public CopyDataUtils(DataItem item) { 
     this.item = item; 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    @Override 
    protected String doInBackground(Void... params) { 

     //...code copy file here 
     return ...; 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 
    } 
} 

所以我的問題是:當actionCopy執行,我想對話框顯示顯示。但沒有發生。執行for(..)後,顯示對話框。 什麼問題?在執行多線程複製數據之前,有沒有關於顯示對話框的建議? 對不起,我的英文不好!

**更新: 我試着把對話框放到AsyncTask中,並顯示在方法onPreExecute中。但沒有任何變化。

回答

0

ü應顯示onPreEcecute)的AsyncTask

public void onPreExecute() 
     { 
      progressDialog = new ProgressDialog(activity); 
      progressDialog.setMessage("your content"); 
      progressDialog.setMax(listData.size()); 
      progressDialog.setProgress(0); 

     for(int i = 0; i < listData.size(); i++) { 
      String value = new CopyDataUtils(listData.get(i)).execute().get(); 

      Log.i(TAG, value); 


      progressDialog.show(); 
     } 


@Override 
    protected String doInBackground(Void... params) { 


     return ...; 
    } 

@Override 
    public void onPostExecute(String s) 
    { 
     progressBar.setProgress(i + 1); 
    } 
+0

什麼是「計算方向」? –

+0

我更新我的答案你應該將其替換爲複製.... –

+0

我試過了。但沒有什麼可改變的。對話框不顯示。 –

0

的進度條(你應該讓你的ProgressBar作爲一個全局變量,然後在的AsyncTask

public void YouActiviy() { 
     ProgressBar progressBar; 
     int i = 0; 
     public onCreate(){ 
      .... 
     } 

     public void actionCopy() { 
      ... 
      progressBar = (ProgressBar) dialog.findViewById(R.id.progress_bar); 
      ... 
      dialog.show(); 
      for(int i = 0; i < listData.size(); i++) { 
       new CopyDataUtils(listData.get(i)).execute(); 
       //progressBar.setProgress(i + 1); 
      } 
     } 

     public class CopyDataUtils extends AsyncTask<Void, Void, String> { 
      ... 
       @Override 
       protected void onPostExecute(String s) { 
        // update your progressbar here 
        progressBar.setProgress(i++); 
        super.onPostExecute(s); 
       } 
     } 
    } 

onPostExcute()更新進度爲方便方式,你按照tutorial這將給你正確的方式使用AsyncTaskProgressBar

希望這有助於

+0

該對話框不能在'for(..)'之前顯示。所以'onPostExecute'中的'setProgress'工作正常嗎? –

+0

你試過了嗎? –

+0

我試過了。但它不是工作 –

0

應設置在onProgressUpdate(進度),試試這個:

protected ProgressDialog mProgressDialog; 

private class copyData extends AsyncTask<String, Integer, String> { 

protected String doInBackground(Void... params) { 
    //copy data here 
    return ...; 
} 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 

    mProgressDialog = new ProgressDialog(YourActivity.this); 
    mProgressDialog.setMessage("Loading..."); 
    mProgressDialog.setIndeterminate(true); 
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
    mProgressDialog.setCancelable(true); 
    mProgressDialog.show(); 
} 

@Override 
protected void onProgressUpdate(Integer... progress) { 
    super.onProgressUpdate(progress); 
    mProgressDialog.setIndeterminate(false); 
    mProgressDialog.setMax(100); 
    mProgressDialog.setProgress(progress[0]); 
} 

protected void onPostExecute(String result) { 
    try { 
     if ((mProgressDialog != null) && mProgressDialog.isShowing()) { 
      mProgressDialog.dismiss(); 
     } 
    } catch (final IllegalArgumentException e) { 
     // Handle or log or ignore 
    } finally { 
     mProgressDialog = null; 
    } 
    } 
}