public class checkSchoolCodeQuery extends AsyncTask<String, Integer, String> {
EditText etSchoolCode = (EditText) findViewById(R.id.etSchoolCode);
String schoolcode = etSchoolCode.getText().toString();
String isValid = "didn run";
@Override
protected String doInBackground(String... params) {
final ParseQuery<ParseObject> query = ParseQuery.getQuery("Schools");
query.whereEqualTo("schoolcode", schoolcode);
query.countInBackground(new CountCallback() {
@Override
public void done(int i, ParseException e) {
if (e == null) {
if (i == 1) {
// Check if expiry date is greater than today's date
query.getFirstInBackground(new GetCallback<ParseObject>() {
@Override
public void done(ParseObject parseObject, ParseException e) {
if (e == null) {
Date expiryDate = parseObject.getDate("expirydate");
Date todaysDate = new Date();
System.out.println(expiryDate);
if (expiryDate.after(todaysDate)) {
isValid = "true";
} else {
isValid = "false";
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext())
.setMessage("Your school doesnt have access");
}
} else {
System.out.println("There was an error when checking if School Code was valid");
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext())
.setMessage("There was an error when checking if School Code was valid");
AlertDialog alertDialog = alertDialogBuilder.show();
isValid = "didnt run";
}
}
});
} else {
isValid = "false";
// Create Alert Dialog to show error text
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext())
.setMessage("The School Code is invalid");
AlertDialog alertDialog = alertDialogBuilder.show();
}
} else {
isValid = "didnt run";
System.out.println("There was an error when checking if School Code was valid");
// Create Alert Dialog to show error text
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext())
.setMessage("There was an error when checking if School Code was valid");
AlertDialog alertDialog = alertDialogBuilder.show();
}
}
});
return isValid;
}
@Override
protected void onPostExecute(String aString) {
if (aString == "true") {
System.out.println("The school code is valid");
} else if (aString == "false") {
System.out.println("The school coe is invalid");
}
else {
System.out.println("There was an error");
}
}
}
When I run my code, the isValid
variable doesn't update, so it uses the initial value of the isValid
string. Am I implementing the AsyncTask
wrongly or did I miss out something? How do I make the variable update in an AsyncTask
? Please help as I am a beginner in programming. I've checked other questions but they don't apply to my case; they are all advanced scenarios and I don't understand their code. I'm just looking for a simple fix to my problem.