即使不满足 if 语句条件,我的 AsyncTask 也会运行
我想显示一个警报对话框并获取有关是否运行 AsyncTask 的用户输入。但是,AsyncTask 无论如何都会运行,即使我将其放在 if 语句中也是如此。有谁知道为什么会发生这种情况?这是我的代码:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Boolean b = false;
AlertDialog.Builder alertbox = new AlertDialog.Builder(
ThisScreen.this);
alertbox.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
b = true;
}
});
alertbox.setPositiveButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
b = false;
}
});
alertbox.setTitle("Title");
alertbox
.setMessage("Continue?");
alertbox.show();
if(b)
new doAsyncTask().execute;
}
});
I want to display an alert dialog and get the user input on whether or not to run the AsyncTask. However the AsyncTask runs anyway, even when I put it in an if statement. Does anyone know why this is happening? This is my code:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Boolean b = false;
AlertDialog.Builder alertbox = new AlertDialog.Builder(
ThisScreen.this);
alertbox.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
b = true;
}
});
alertbox.setPositiveButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
b = false;
}
});
alertbox.setTitle("Title");
alertbox
.setMessage("Continue?");
alertbox.show();
if(b)
new doAsyncTask().execute;
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道这是否有什么区别,但合乎逻辑的是使用 setNegativeButton() 方法设置无按钮。
I don't know if it makes any difference but the logical thing would be to set the no-button using the method setNegativeButton().
您应该移动代码以在内部运行任务
,顺便说一句,我不确定您提供的代码是否会编译:)
You should move the code to run the task inside
btw I'm not sure that the code you provided will ever compile :)