0

I am trying to complete my login dialog in my application and I have problems with handling my dialogs.

I have three dialogs, first an AlertDialog as a login dialog, second a progressDialog and an alertDialog which displays an error message. Here is my code:

protected Dialog onCreateDialog(int id) {
    switch (id) {
    case KBConstants.DIALOG_LOGIN_ID:
        TextView textUsername;
        TextView textPassword;
        TextView textSave;
        // Here I build my Layout
        ...
        // return dialog
        return new AlertDialog.Builder(this).setTitle(R.string.login_title).setMessage(R.string.login_introduction).setView(loginLayout).setIcon(R.drawable.kreative_barometer_72x72)
                .setPositiveButton(R.string.login_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        final DialogInterface d = dialog;
                        if (checkBoxSave.isChecked()) {
                            setRememberLogin(true);
                        } else {
                            setRememberLogin(false);
                        }
                        waitConnect = ProgressDialog.show(KreativBarometerMainView.this, getString(R.string.login_connect_title), getString(R.string.login_connect_introduction), false, true);
                        Thread t1 = new Thread() {
                            public void run() {
                                Looper.prepare();
                                DataStruct dataStruct = null;
                                try {
                                    dataStruct = SoapHandler.makeCall(SoapHashMap.CHECK_USER_LOGIN, mainService.getSoapMap(), editUsername.getText().toString(), editPassword.getText().toString(), null, null);
                                    ServerMessageStruct sms = (ServerMessageStruct) dataStruct;
                                    if (sms.getMsgType() == 1) {
                                        mainService.setUserValidation(true);
                                        setLogin(editUsername.getText().toString(), editPassword.getText().toString());
                                    } else {
                                        mainService.setUserValidation(false);

                                        setLogin(null, null);
                                                                                }
                                } catch (IOException ioe) {
                                    mainService.setUserValidation(false);
                                    setLogin(null, null);

                                } catch(XmlPullParserException xmlppe){

                                } finally{
                                    if(!mainService.isUserValidated()){
                                        waitConnect.dismiss();
                                        showDialog(KBConstants.DIALOG_NO_LOGIN_USER_ID);
                                    }
                                }
                                waitConnect.dismiss();
                            }
                        };
                        t1.start();

                    }
                }).setNeutralButton(R.string.login_setup, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked setup so do some stuff */
                        startActivity(new Intent(packageContext, SettingView.class));
                    }
                }).setNegativeButton(R.string.login_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked cancel so do some stuff */
                        KreativBarometerMainView.this.finish();
                    }
                }).setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        KreativBarometerMainView.this.finish();

                    }
                }).create();
    case KBConstants.DIALOG_NO_LOGIN_USER_ID:
        return new AlertDialog.Builder(this).setMessage(R.string.login_info_error_user).setIcon(R.drawable.kreative_barometer_72x72).setTitle(R.string.login_error)
                .setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        showDialog(KBConstants.DIALOG_LOGIN_ID);
                    }
                }).create();
    }
    return null;
} 

When a user clicks on "connect" (positive button) a SOAP call is made to consume a webservice which validates the user data. While the user is waiting for the call to be made, the progressDialog is shown to indicate the work. After the call has been made, the DIALOG_NO_LOGIN_USER_ID Dialog should be shown if the user data was not valid. The user can click on okay and the login dialog is shown again.

But the DIALOG_NO_LOGIN_USER_ID dialog is not showing. I tried to call the showDialog() after the Thread was started, but then there are threading issues. The dialog is shown, but the result of the validation is ignored.

Can anyone help me to understand how to manage the dialogs? Is there any other, perhaps "better" solution for using an AlertDialog as a login dialog?

regards, htz

4

2 回答 2

0

好吧,首先,您不需要在另一个对话框中调用对话框。您所做的只是将 onClickListener 设置为对话框按钮并相应地执行操作。例如,当用户单击您时,"connect" (positive button)您将调用方法中的下一个对话框onClick()

但是,如果您只想这样,那么您需要一个Activity Context可以作为参数传递给自定义对话框并使用该上下文调用新对话框的参数。

于 2012-04-05T14:37:03.820 回答
0

这个问题的解决方案是利用类

android.os.AsyncTask<Params, Progress, Result>

JavaDoc 中给出了最好的描述:

“AsyncTask 可以正确和轻松地使用 UI 线程。此类允许在 UI 线程上执行后台操作和发布结果,而无需操作线程和/或处理程序。”

通过使用这些方法onPreExecute(),您可以在 UI 线程上操作 View。

完成此方法后,doInBackground()将调用 并在此处开始您的后台操作。此方法不适用于 UI 线程,因此它不会阻塞您的应用程序。

onPostExecute()调用它之后,您可以使用您的计算结果。

我的问题是在进行后台操作时正确显示进度指示器。这可以通过使用onProgressUpdate()在 UI 线程上工作的方法来完成,同时进行后台计算。

于 2012-05-09T11:33:27.067 回答