0

ユーザーにログイン ダイアログ (カスタム ビューのカスタム ダイアログ) を表示しようとしています。[ログイン] をクリックすると、ProgressDialog を表示し、ログインが完了したら閉じます。

問題は、ProgressDialog を表示できないことです。

これが私のコードです:

    private final MyActivity activity;

    public MyPresenter(MyActivity activity) {
        this.activity = activity;
    }

    public Dialog getLoginSignupDialog() {
        final Dialog dialog = new Dialog(activity);
        dialog.setContentView(R.layout.dialog_signup);
        dialog.setTitle("Login");
        final TextView email = (TextView)dialog.findViewById(R.id.textViewEmail);
        final TextView password = (TextView)dialog.findViewById(R.id.textViewPassword);
        Button signupButton = (Button)dialog.findViewById(R.id.buttonSignup);           
        signupButton.setOnClickListener(new OnClickListener() {                 
            public void onClick(View v) {   
                ProgressDialog progressDialog = null;

                try {
                    progressDialog = ProgressDialog.show(activity, "", "Signing up...",false);
                    activity.signup(email.getText().toString(), password.getText().toString());
                    progressDialog.dismiss();
                    dialog.dismiss();
                } catch (ParseException e) {
                    if (progressDialog != null) progressDialog.dismiss();
                    Toast.makeText(activity, "Sorry, an error occured.", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
        });     
        return dialog;
    }

このコードはプレゼンターにあり、メイン アクティビティ インスタンスが渡されます。UIロジックを分離しておくためだけに。

主なアクティビティ ( onCreate) では、次のことを行います。

showDialog(DIALOG_SIGNUP);

onCreateDialog次に、メソッドをオーバーライドします。

@Override
protected Dialog onCreateDialog(int id) {   
    Dialog dialog = null;   
    switch (id) {
        case DIALOG_SIGNUP: {
            dialog = presenter.getLoginSignupDialog();                              
            break;
        }
    }   
    return dialog;

}

もう 1 つのダイアログ (カスタム ログイン 1 つ) は正常に表示され、すべてが機能し、閉じます。しかし、ProgressDialog を表示できません。

私は何を間違っていますか?

4

2 に答える 2

0

次のように、AsyncTaskとしてサインアップアクティビティを実行する必要があります。

    new AsyncTask<String, Void, Void>() {


        ProgressDialog progressDialog=null;


        @Override
        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(activity, "", "Signing up...",false);
        }

        @Override
        protected Void doInBackground(String... params) {
            try {
                activity.signup(params[0], params[1]);

            } catch (ParseException e) {
                Toast.makeText(activity, "Sorry, an error occured.", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if (progressDialog != null) progressDialog.dismiss();
            dialog.dismiss();
        }
    }.execute(email.getText().toString(), password.getText().toString());
于 2012-08-22T03:48:19.510 に答える
0

進行状況ダイアログを関数 activity.signup() に入れて、そこで閉じると役立つと思います。これにより、メインの UI スレッドにダイアログが作成されます。

おそらく、ログイン プロセスに AsyncTask を使用する必要があります。

于 2012-08-22T03:27:40.100 に答える