2012-09-21 48 views
0

我有一個按鈕,顯示一個AlertDialog女巫我做了一些計算,它需要一段時間,我想在進行計算時顯示進度條。 這是我迄今爲止,但它不是像我想要的那樣工作。 對話節目,但AlertDialog前絕望做完計算progressbar while loading AlerDialogBox

這個代碼

public void OnClick(View v) { 

    try { 

     new DownloadingProgressTask().execute(); 

    } catch (Exception e) { 

     Log.v("Exception", e.toString()); 
    } 

} 
    private class DownloadingProgressTask extends 
     AsyncTask<String, Void, Boolean> { 

    private ProgressDialog dialog = new ProgressDialog(Calculations.this); 

    /** progress dialog to show user that the backup is processing. */ 

    /** application context. */ 

    protected void onPreExecute() { 
     this.dialog.setMessage("Please wait"); 
     this.dialog.show(); 

    } 

    @SuppressWarnings("deprecation") 
    protected Boolean doInBackground(final String... args) { 
     try { 

      LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext() 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      inflatedview = layoutInflater.inflate(R.layout.calculationhelp, null); 

      final TextView result = (TextView) inflatedview.findViewById(R.id.textViewresult); 
      titleAlerDialog = (TextView) inflatedview.findViewById(R.id.textViewTitle); 
      messageAlerDialog = (TextView) inflatedview.findViewById(R.id.textViewMsg); 

      separated = help.toString().split("#"); 

      Resources r = getResources(); 
      Bundle extras = getIntent().getExtras(); 
      if (extras != null) { 

         tableResults = r.getStringArray(R.array.Calculo1Results1); 
         result.setText(r.getString(R.string.calculo1results1)); 
      } 

      titleAlerDialog.setText(separated[0]); 

      messageAlerDialog.setText(separated[1].toString()); 


      /* Find Tablelayout defined in main.xml */ 
      TableLayout tl = (TableLayout) inflatedview.findViewById(R.id.myTableLayout); 
      tl.setColumnStretchable(0, true); 
      tl.setColumnStretchable(1, true); 
      tl.setColumnStretchable(2, true); 

      for (int i = 0; i < tableResults.length; i++) { 
       /* Create a new row to be added. */ 
       TableRow tr = new TableRow(Calculations.this); 
       tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

       /* Create a cell1 to be the row-content. */ 
       TextView cell1 = new TextView(Calculations.this); 
       TextView cell2 = new TextView(Calculations.this); 
       TextView cell3 = new TextView(Calculations.this); 

       String[] cells = tableResults[i].toString().split("@"); 

       cell1.setWidth(130); 
       cell1.setGravity(Gravity.CENTER_VERTICAL 
         | Gravity.CENTER_HORIZONTAL); 
       cell1.setText(cells[0].toString()); 
       cell1.setBackgroundResource(R.drawable.celda_cuerpo); 
       cell1.setLayoutParams(new LayoutParams(
         LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

       cell2.setWidth(130); 
       cell2.setGravity(Gravity.CENTER_VERTICAL 
         | Gravity.CENTER_HORIZONTAL); 
       cell2.setText(cells[1].toString()); 
       cell2.setBackgroundResource(R.drawable.celda_cuerpo); 
       cell2.setLayoutParams(new LayoutParams(
         LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

       cell3.setWidth(130); 
       cell3.setGravity(Gravity.CENTER_VERTICAL 
         | Gravity.CENTER_HORIZONTAL); 
       cell3.setText(cells[2].toString()); 
       cell3.setBackgroundResource(R.drawable.celda_cuerpo); 
       cell3.setLayoutParams(new LayoutParams(
         LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

       /* Add cell1 to row. */ 
       tr.addView(cell1); 
       tr.addView(cell2); 
       tr.addView(cell3); 

       /* Add row to TableLayout. */ 
       tl.addView(tr, new TableLayout.LayoutParams(
         LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

      } 
      //dialog.dismiss(); 
      return true; 
     } catch (Exception e) { 
      Log.e("tag", "error", e); 
      return false; 
     } 
    } 

@Override 
protected void onPostExecute(final Boolean success) { 

if (dialog.isShowing()) { 
    dialog.dismiss(); 
} 

if (success) { 
    AlertDialog.Builder dialog1 = new AlertDialog.Builder(Calculations.this); 
    dialog1.setNegativeButton("Close", 
      new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface arg0, int arg1) { 

       } 
      }); 

    dialog1.setView(inflatedview); 
    final AlertDialog alert = dialog1.create(); 
    alert.requestWindowFeature(Window.FEATURE_NO_TITLE); 

    alert.show(); 



    ((Button) alert.findViewById(android.R.id.button2)) 
      .setBackgroundResource(R.drawable.custom_button_red); 
    ((Button) alert.findViewById(android.R.id.button2)) 
      .setTextColor(Color.WHITE); 


} else { 
    Toast.makeText(Calculations.this, "Error", Toast.LENGTH_LONG) 
      .show(); 
} 
} 

} 

任何幫助,將不勝感激 問候 HP

回答

0

嘗試使用以下。我已經爲接受Context的AsyncTask添加了一個構造函數,並將其用於創建視圖等。我還將第二個警報對話框的創建移動到doInBackground()中,並僅保留顯示它的代碼onPostExecute()。它應該加快速度。

public void OnClick(View v) { 

    try { 

     new DownloadingProgressTask(getBaseContext()).execute(); 

    } catch (Exception e) { 

     Log.v("Exception", e.toString()); 
    } 

} 

private class DownloadingProgressTask extends AsyncTask<String, Void, Boolean> { 

    Context mContext; 

    public DownloadingProgressTask(Context c) { 
     mContext = c; 
    } 

    private ProgressDialog dialog = new ProgressDialog(mContext); 

    final AlertDialog alert; 
    /** progress dialog to show user that the backup is processing. */ 

    /** application context. */ 

    protected void onPreExecute() { 
     this.dialog.setMessage("Please wait"); 
     this.dialog.show(); 

    } 

    @SuppressWarnings("deprecation") 
    protected Boolean doInBackground(final String... args) { 
     try { 

      LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(
        Context.LAYOUT_INFLATER_SERVICE); 
      inflatedview = layoutInflater.inflate(R.layout.calculationhelp, null); 

      final TextView result = (TextView) inflatedview.findViewById(R.id.textViewresult); 
      titleAlerDialog = (TextView) inflatedview.findViewById(R.id.textViewTitle); 
      messageAlerDialog = (TextView) inflatedview.findViewById(R.id.textViewMsg); 

      separated = help.toString().split("#"); 

      Resources r = getResources(); 
      Bundle extras = getIntent().getExtras(); 
      if (extras != null) { 

       tableResults = r.getStringArray(R.array.Calculo1Results1); 
       result.setText(r.getString(R.string.calculo1results1)); 
      } 

      titleAlerDialog.setText(separated[0]); 

      messageAlerDialog.setText(separated[1].toString()); 

      /* Find Tablelayout defined in main.xml */ 
      TableLayout tl = (TableLayout) inflatedview.findViewById(R.id.myTableLayout); 
      tl.setColumnStretchable(0, true); 
      tl.setColumnStretchable(1, true); 
      tl.setColumnStretchable(2, true); 

      for (int i = 0; i < tableResults.length; i++) { 
       /* Create a new row to be added. */ 
       TableRow tr = new TableRow(mContext); 
       tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

       /* Create a cell1 to be the row-content. */ 
       TextView cell1 = new TextView(mContext); 
       TextView cell2 = new TextView(mContext); 
       TextView cell3 = new TextView(mContext); 

       String[] cells = tableResults[i].toString().split("@"); 

       cell1.setWidth(130); 
       cell1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL); 
       cell1.setText(cells[0].toString()); 
       cell1.setBackgroundResource(R.drawable.celda_cuerpo); 
       cell1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

       cell2.setWidth(130); 
       cell2.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL); 
       cell2.setText(cells[1].toString()); 
       cell2.setBackgroundResource(R.drawable.celda_cuerpo); 
       cell2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

       cell3.setWidth(130); 
       cell3.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL); 
       cell3.setText(cells[2].toString()); 
       cell3.setBackgroundResource(R.drawable.celda_cuerpo); 
       cell3.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

       /* Add cell1 to row. */ 
       tr.addView(cell1); 
       tr.addView(cell2); 
       tr.addView(cell3); 

       /* Add row to TableLayout. */ 
       tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

       AlertDialog.Builder dialog1 = new AlertDialog.Builder(mContext); 
       dialog1.setNegativeButton("Close", new DialogInterface.OnClickListener() { 

        public void onClick(DialogInterface arg0, int arg1) { 

        } 
       }); 

       dialog1.setView(inflatedview); 
       alert = dialog1.create(); 
       alert.requestWindowFeature(Window.FEATURE_NO_TITLE); 
      } 
      // dialog.dismiss(); 
      return true; 
     } catch (Exception e) { 
      Log.e("tag", "error", e); 
      return false; 
     } 
    } 

    @Override 
    protected void onPostExecute(final Boolean success) { 

     if (success) { 
      alert.show(); 

      ((Button) alert.findViewById(android.R.id.button2)).setBackgroundResource(R.drawable.custom_button_red); 
      ((Button) alert.findViewById(android.R.id.button2)).setTextColor(Color.WHITE); 

     } else { 
      Toast.makeText(mContext, "Error", Toast.LENGTH_LONG).show(); 
     } 

     if (dialog.isShowing()) { 
      dialog.dismiss(); 
     } 

    } 
} 
+0

之後代碼change.it表現得像它假設,但狙擊手只是凍結,直到計算完成。 – user749015

+0

我正在進行另一次編輯。不掛斷。 –

+0

@ user749015立即嘗試。 –