我onPostExecute()在AsyncTask课堂上的方法有问题。
我有一个SignupActivity:
public class SignupActivity extends AppCompatActivity implements SignupListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signup_activity);
//new task, i pass context and interface to it
signup = new Signup(getApplicationContext(), this);
signupButon.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(validate()) {
try {
//new task every click
Signup newSignup = new Signup(signup);
//here start AsyncTask
newSignup.execute(name, email, password).get();
} catch (Exception e) {
Toast.makeText(ERROR);
}
// if sign up succes, == true;
if(signupValid) {
Toast.makeText(SUCCES);
finish();
} else {
Toast.makeText(ERROR);
}
}
}
});
}
// my own interface for getting result as bool from onPostExecute
@Override
public void onSignupPerformed(Boolean result){ this.signupValid = result; }
这实现了我从以下位置捕获结果的接口onPostExecute():
public interface SignupListener{
void onSignupPerformed(Boolean result);
}
现在,AsyncTask我在代码中触发:
public class Signup extends AsyncTask<String, Boolean, Boolean> {
public Signup(Context context, SignupListener listener){
db = ApplicationDatabase.getDatabase(context);
this.context = context;
this.listener = listener;
}
public Signup(Signup signup){
//constructor to make new task based on first task
db = signup.db;
context = signup.context;
listener = signup.listener;
}
我的问题是,为什么当我第一次单击按钮时,func 返回Boolean.TRUE但在SignupActivity signupValid变量中是false(注册表单未退出,但用户已添加到 DB),但是当我第二次单击注册按钮时,ofc 注册失败(因为我们创建了新用户几秒钟前)但signupValid更改为true并通过注册表单?我需要单击SignupButton两次才能最终退出表单。感谢您在我的代码中发现错误
牧羊人nacy
相关分类