2

这是我连接到对话框的代码部分。他们按下按钮后,它应该显示出来,显示后,它应该处理数据,完成后,它应该隐藏起来。但它甚至没有出现?

ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Prosimo počakajte da naloži podatke.");
dialog.setIndeterminate(false);
dialog.setCancelable(false);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

private Button.OnClickListener listener = new Button.OnClickListener() {
    public void onClick(View v){
        if(selectedClass >= 0){
            dialog.show();

            ... data processing ...

            Intent firstUpdate = new Intent(context, ConfigurationActivity.class);
            firstUpdate.setAction("android.appwidget.action.APPWIDGET_ENABLED");
            firstUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widget_id);
            context.sendBroadcast(firstUpdate);

            dialog.dismiss();
            setResult(RESULT_OK, firstUpdate); 
            finish(); 
        } else {
            Log.i("Schedule", "Missing selections");
        }
    }
};

谢谢你的帮助。

4

1 回答 1

4

多亏了“ Pragnani”,我才得以成功。这是最终代码:

private class ProgressTask extends AsyncTask<String, Void, Boolean> 
{
    private ProgressDialog dialog;
    private ConfigurationActivity activity;

    public ProgressTask(ConfigurationActivity activity) 
    {
        this.activity = activity;
        context = activity;
        dialog = new ProgressDialog(context);
    }

    private Context context;

    protected void onPreExecute() 
    {
        dialog = new ProgressDialog(context);
        dialog.setMessage("Prosimo počakajte da naloži podatke.");
        dialog.setIndeterminate(false);
        dialog.setCancelable(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.show();
    }

    @Override
    protected void onPostExecute(final Boolean success) 
    {
        if (dialog.isShowing()) 
                    {
            dialog.dismiss();
        }
        if (success) 
                    {
            Toast.makeText(context, "OK", Toast.LENGTH_LONG).show();
        } 
                    else 
                    {
            Toast.makeText(context, "ERROR", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected Boolean doInBackground(final String... args) 
    {
        try {    
            ... processing ...

            return true;
        } catch (Exception e){
            Log.e("Schedule", "UpdateSchedule failed", e);
            return false;
        }
    }

}

调用类:

new ProgressTask(ConfigurationActivity.this).execute();

谢谢普拉尼亚尼!

于 2013-04-22T16:59:00.447 回答