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