Android:当长按列表视图中的项目时使用 AlertDialog
我有一个由列表视图创建的项目列表。我想长按列表中的一项并打开一个警报对话框,并根据该对话框上的是或否键我想设置一个全局变量。我正在使用的代码位于“MyActivity.java”内部,如下所示:
ListView lv = getListView();
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> av, View v, int pos, final long id) {
final AlertDialog.Builder b = new AlertDialog.Builder(MyActivity.this);
b.setIcon(android.R.drawable.ic_dialog_alert);
b.setMessage("Are you sure?");
b.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
yesOrNo = 1;
}
});
b.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
yesOrNo = 0;
}
});
b.show();
if (yesOrNo == 1) {
DO SOMETHING;
}
return true;
}
});
但是,无论我按“是”还是“否”,全局变量“yesOrNo”都不会改变。 有人可以让我知道代码有什么问题吗?
感谢您的帮助。
I have a list of items, created by a listview. I would like to long press one of the items on the list and an alert dialog to open up and depending on yes or no key on that dialog box I wan to set a global variable. The code that I am using is inside "MyActivity.java" and looks like this:
ListView lv = getListView();
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> av, View v, int pos, final long id) {
final AlertDialog.Builder b = new AlertDialog.Builder(MyActivity.this);
b.setIcon(android.R.drawable.ic_dialog_alert);
b.setMessage("Are you sure?");
b.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
yesOrNo = 1;
}
});
b.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
yesOrNo = 0;
}
});
b.show();
if (yesOrNo == 1) {
DO SOMETHING;
}
return true;
}
});
However, the global variable "yesOrNo" is not changing no matter if I press "Yes" or "No".
Can somebody let me know what is wrong with the code?
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
AlertDialog
不会等待选择。调用show()
方法后,这两行代码将立即执行:因此
yesOrNo
变量的值将为其初始值。解决方案:
可以在 PositiveButton 的
onClick()
方法中调用doSomething(0)
,在negativeButton 的onClick()
方法。AlertDialog
does not wait for the selection. After you callshow()
method, these two lines will be executed immediately:So value of
yesOrNo
variable will be its initial value.Solution:
You can call
doSomething(0)
inonClick()
method of positiveButton anddoSomething(1)
inonClick()
method of negativeButton.yesOrNo 正在更改。但是您无法捕获它。因为 AlertDialog 是异步的,所以它不会等待单击。它会执行范围的其余部分。如果您想查看更改,请查看单击对话框上的值按钮。然后你会看到
yesOrNo is changing.But you couldn't catch it.Because AlertDialog is asynchronous it doesn't wait for the click.It execute the rest part of the scope.If you want to see the change then see the value on click on the dialog button .then you will see
调用 b.show() 后无法立即检查 yesOrNo 的值。现在显示对话框并不意味着已单击按钮。您应该在
OnClickListener
中执行DO SOMETHING
操作,或者从OnClickListener
中调用方法。you cannot check the value of yesOrNo right after you have called b.show(). Just because the dialog is now shown, doesn't mean a button has been clicked. You should do your
DO SOMETHING
inside theOnClickListener
or call a method from within theOnClickListener
.以下测试不在正确的位置:
它是在创建对话框后进行评估的,而不是在用户单击按钮后进行评估。所以当时
yesOrNo
仍然是false
,而且我们从不DO SEOMTHING
。DO SOMETHING
应位于b.setPositiveButton()
的onClick()
处理程序中。The following test is not at the right place:
It is evaluated once your dialog is created, and not once the user clicks a button. So
yesOrNo
is stillfalse
at that time, and we neverDO SEOMTHING
.DO SOMETHING
should be located inb.setPositiveButton()
'sonClick()
handler.在 PositiveButton 和 NegativeButton 中调用两个单独的函数,然后编写所需的代码:
示例:
Call two separate functions in positivebutton and in negativebutton, and write the code what you want:
sample: