加载AlerDialogBox时的进度条

时间:2012-09-21 17:38:04

标签: android

我有一个显示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();
}
}

}

任何帮助将不胜感激 最好的祝福 HP

1 个答案:

答案 0 :(得分:0)

尝试使用以下内容。我已经在你的AsyncTask中添加了一个接受Context的构造函数,并在整个过程中使用它来创建你的视图等。我还将第二个警告对话框的创建移动到了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();
        }

    }
}