我有两个类:checkenrol.java和allcourses.java
checkenrol扩展了AsyncTask。我在checkenrol中也有一个名为“alreadyEnrolled”的全局布尔值。
allcourses将使用2个参数执行checkenrol。
这个想法是:
我的问题是Asynctask的执行和布尔的比较是同时完成的。
这里进一步说明我的代码片段:
这是我的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;
}
...
} ...
这是我的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");
}
当我点击我已经注册的项目时,它输出“尚未注册”但第二次点击它时,它将输出“已经注册”。
对不起,很长的帖子。希望你能帮助我:)。
答案 0 :(得分:0)
您应该在onPreExecute()
扩展AsyncTask
class
方法
编辑:
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
}
}
答案 1 :(得分:0)
将你要比较的内容放在后执行中。这通常会在后台任务结束后执行......
public class checkenrol扩展了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");
}
}
}