Android ProgressDialog没有显示出来

时间:2013-04-22 16:26:43

标签: android button android-activity progressdialog

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

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");
        }
    }
};

感谢您的帮助。

1 个答案:

答案 0 :(得分: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();

感谢Pragnani!