I have 2 classes: checkenrol.java and allcourses.java
checkenrol extends AsyncTask. I also have a global boolean named "alreadyEnrolled" in checkenrol.
allcourses will execute checkenrol with 2 parameters.
The idea is that:
My problem is that the execution of the Asynctask and the comparing of the boolean is done simultaneously.
To further illustrate here are fragments of my code:
This is my checkenrol.java
public class checkenrol extends AsyncTask <String, Void, String> {
public static boolean alreadyEnrolled = false;
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
...
if (res.equals("1")) {
alreadyEnrolled = true;
}
else {
alreadyEnrolled = false;
}
...
} ...
This is my allcourses.java
checkenrol ce = new checkenrol();
ce.execute(param1, param2);
if (checkenrol.alreadyEnrolled == true) {
//redirect to subject's home
System.out.println("Already Enrolled");
}
else if (checkenrol.alreadyEnrolled == false){
System.out.println("Not yet enrolled");
}
When I click on the item that I am ALREADY enrolled to, it outputs "Not yet enrolled" but the second time I click it, it will output "Already enrolled".
Sorry for the long post. hope you will help me :)
You should make use of onPreExecute()
method in your AsyncTask
extending class
EDIT:
public class checkenrol extends AsyncTask <String, Void, String> {
@Override
protected String doInBackground(String... params) {
}
@Override
protected String onPreExecute() {
//Before starting doInBackground()
//Will run in main thread
}
@Override
protected String onPostExecute() {
//After completing doInBackground()
//Will run in main thread
}
}
Put what you to compare is in the post execute..This will usually execute after the background task will over...
public class checkenrol extends AsyncTask {
public static boolean alreadyEnrolled = false;
protected String doInBackground(String... params) {
if (res.equals("1")) {
alreadyEnrolled = true;
}
else {
alreadyEnrolled = false;
}
return alreadyEnrolled;
}
protected String onPostExecute(String r) {
checkenrol ce = new checkenrol(); ce.execute(param1, param2);
if (checkenrol.alreadyEnrolled == true) {
//redirect to subject's home
System.out.println("Already Enrolled");
}
else if (checkenrol.alreadyEnrolled == false){
System.out.println("Not yet enrolled");
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.