Reputation: 557
Hi I am having a bit of trouble with the AsyncTask class for android. Here is my code for logging in a user on Parse. There are different cases such when the password is incorrect, etc.
My problem is that the error checking works and the different cases work fine but the progressDialog button will not go away after i close the alertdialog... Can anybody help me with this?
Here is my code
private class LoadTask extends AsyncTask<Map<String, String>, Integer, Void> {
// called before running code in a separate thread
private Result resultCode;
private boolean isSuccess;
private ProgressDialog progressDialog;
public LoadTask(Activity activity) {
onPreExecute();
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(TitlePage.this,
getString(R.string.login_progress_title),
getString(R.string.login_progress_message), false, false);
}
@Override
protected Void doInBackground(Map<String, String>... arg0) {
// Try to login with the given inputs
ParseUser user = null;
Map<String, String> argMap = arg0[0];
try {
user = ParseUser.logIn(argMap.get("username"), argMap.get("password"));
} catch (ParseException e) {
e.fillInStackTrace();
boolean errorOccured = false;
List<ParseObject> usernameResults = new ArrayList<ParseObject>();
List<ParseObject> passwordResults = new ArrayList<ParseObject>();
ParseQuery query = ParseUser.getQuery();
// try to find the username that the user typed in
query.whereEqualTo("username", argMap.get("username"));
try {
query.count();
usernameResults = query.find();
} catch (ParseException e1) {
// error occured trying to find the username
errorOccured = true;
e1.printStackTrace();
} catch (NullPointerException e1) {
errorOccured = true;
e1.printStackTrace();
}
// try to find the password that the user typed in
// associated with that username
query.whereEqualTo("username", argMap.get("username"));
query.whereEqualTo("password", argMap.get("password"));
try {
query.count();
passwordResults = query.find();
} catch (ParseException e1) {
// error occured trying to find the password
errorOccured = true;
e1.printStackTrace();
} catch (NullPointerException e1) {
errorOccured = true;
e1.printStackTrace();
}
// figure out the error
if (errorOccured) {
resultCode = Result.UNEXPECTED_ERROR;
// buildAlertDialog(R.string.error_login_title, R.string.error_login_unexp);
}
if ((usernameResults.size() == 0) && (passwordResults.size() == 0)) {
resultCode = Result.BOTH_INCORRECT;
// buildAlertDialog(R.string.error_login_title, R.string.error_login_combo);
} else if ((usernameResults.size() == 0) && (passwordResults.size() != 0)) {
resultCode = Result.USERNAME_INCORRECT;
//buildAlertDialog(R.string.error_login_title, R.string.error_login_uname);
} else if ((usernameResults.size() != 0) && (passwordResults.size() == 0)) {
resultCode = Result.PASSWORD_INCORRECT;
//buildAlertDialog(R.string.error_login_title, R.string.error_login_pswd);
} else {
// unexpected error
resultCode = Result.UNEXPECTED_ERROR;
// buildAlertDialog(R.string.error_login_title, R.string.error_login_unexp);
}
isSuccess = false;
return null;
}
// Check for verified email
boolean emailVerified = user.getBoolean("emailVerified");
if (!emailVerified) {
resultCode = Result.EMAIL_NOT_VERIFIED;
ParseUser.logOut();
}
isSuccess = true;
return null;
}
@Override
protected void onPostExecute(Void result) {
progressDialog.dismiss();
if (isSuccess) {
TitlePage.this.setReturnStatus(isSuccess);
} else {
System.out.println("THIS IS RESULT CODE " + resultCode);
if (resultCode == Result.UNEXPECTED_ERROR) {
buildAlertDialog(R.string.error_login_title, R.string.error_login_unexp);
} else if (resultCode == Result.BOTH_INCORRECT) {
buildAlertDialog(R.string.error_login_title, R.string.error_login_combo);
} else if (resultCode == Result.USERNAME_INCORRECT) {
buildAlertDialog(R.string.error_login_title, R.string.error_login_uname);
} else if (resultCode == Result.PASSWORD_INCORRECT) {
buildAlertDialog(R.string.error_login_title, R.string.error_login_pswd);
} else {
buildAlertDialog(R.string.error_login_title, R.string.error_login_verif);
}
TitlePage.this.setReturnStatus(isSuccess);
}
}
}
And here is my code to run the async task in my main activity
Map<String, String> argMap = new HashMap<String, String>();
argMap.put("username", usernameString);
argMap.put("password", passwordString);
LoadTask task = new LoadTask(this);
task.execute(argMap);
private void buildAlertDialog(final int title, final int message) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
// set title
alertDialogBuilder.setTitle(title);
// set dialog message
alertDialogBuilder
.setMessage(message)
.setCancelable(false)
.setPositiveButton(R.string.close_alert, new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
// if this button is clicked, close the dialog box
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show the message
alertDialog.show();
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
Thank you
Upvotes: 0
Views: 175
Reputation: 578
we can make progress dialog as same in the following example :
@Override
protected void onPreExecute() {
loadingDailog = new ProgressDialog(context,AlertDialog.THEME_HOLO_LIGHT);
((ProgressDialog) loadingDailog).setIndeterminate(true);
((ProgressDialog) loadingDailog).setProgressStyle(ProgressDialog.THEME_HOLO_LIGHT);
loadingDailog.setMessage("Loading...");
loadingDailog.show();
}
when you want to disapear this bar:
if (loadingDailog != null && loadingDailog.isShowing()) {
loadingDailog.dismiss();
}
Upvotes: 2