gpt4 book ai didi

java - 异步任务,关闭alertdialog后进度对话框不会消失

转载 作者:行者123 更新时间:2023-12-02 06:47:21 25 4
gpt4 key购买 nike

您好,我在使用 Android 的 AsyncTask 类时遇到了一些问题。这是我在 Parse 上登录用户的代码。存在密码错误等不同情况。

我的问题是错误检查有效并且不同的情况工作正常,但在我关闭警报对话框后,进度对话框按钮不会消失...任何人都可以帮助我吗?

这是我的代码

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

这是我在主要 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();
}
}

谢谢

最佳答案

我们可以像下面的例子一样制作进度对话框:

@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();
}

当你想消失这个栏时:

 if (loadingDailog != null && loadingDailog.isShowing()) {
loadingDailog.dismiss();
}

关于java - 异步任务,关闭alertdialog后进度对话框不会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18479911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com