我有一个按钮,在女巫我做一些计算时显示一个 AlertDialog,这需要一些时间,我想在计算时显示一个进度条。这是我到目前为止所拥有的,但它并没有像我想要的那样工作。对话框显示,但在 AlertDialog 完成计算之前感到绝望
这是代码
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();
}
}
}
任何帮助将不胜感激