简体   繁体   English

Android ProgressDialog未显示

[英]Android ProgressDialog not showing up

Here is the part of my code connected to the dialog. 这是我的代码与对话框连接的部分。 After they press the button, it should show up and after showing up, the it should process the data and when its done, it should hide away. 当他们按下按钮后,它应该显示出来,并且在显示之后,它应该处理数据,完成后应该隐藏起来。 But it doesen't even appear? 但是它甚至没有出现?

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

Thanks for any help. 谢谢你的帮助。

Thanks to "Pragnani" I have managed to make it work. 多亏了“ Pragnani”,我才得以使它工作。 Here is the final code: 这是最终代码:

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

}

Calling the class: 叫课:

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

Thanks Pragnani ! 谢谢普拉尼亚尼!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM