Android:当长按列表视图中的项目时使用 AlertDialog

发布于 2024-11-25 09:44:47 字数 1146 浏览 0 评论 0原文

我有一个由列表视图创建的项目列表。我想长按列表中的一项并打开一个警报对话框,并根据该对话框上的是或否键我想设置一个全局变量。我正在使用的代码位于“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

笨笨の傻瓜 2024-12-02 09:44:48

AlertDialog 不会等待选择。调用 show() 方法后,这两行代码将立即执行:

if (yesOrNo == 1) {
        DO SOMETHING;   
}

因此 yesOrNo 变量的值将为其初始值。

解决方案

可以在 PositiveButton 的 onClick() 方法中调用 doSomething(0) ,在negativeButton 的 onClick() 方法。

AlertDialog does not wait for the selection. After you call show() method, these two lines will be executed immediately:

if (yesOrNo == 1) {
        DO SOMETHING;   
}

So value of yesOrNo variable will be its initial value.

Solution:

You can call doSomething(0) in onClick() method of positiveButton and doSomething(1) in onClick() method of negativeButton.

深者入戏 2024-12-02 09:44:48

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

多情出卖 2024-12-02 09:44:48

调用 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 the OnClickListener or call a method from within the OnClickListener.

北方的韩爷 2024-12-02 09:44:48

以下测试不在正确的位置:

if (yesOrNo == 1) {
    DO SOMETHING;   
}

它是在创建对话框后进行评估的,而不是在用户单击按钮后进行评估。所以当时 yesOrNo 仍然是 false,而且我们从不DO SEOMTHING

DO SOMETHING 应位于 b.setPositiveButton()onClick() 处理程序中。

The following test is not at the right place:

if (yesOrNo == 1) {
    DO SOMETHING;   
}

It is evaluated once your dialog is created, and not once the user clicks a button. So yesOrNo is still false at that time, and we never DO SEOMTHING.

DO SOMETHING should be located in b.setPositiveButton()'s onClick() handler.

反目相谮 2024-12-02 09:44:48

在 PositiveButton 和 NegativeButton 中调用两个单独的函数,然后编写所需的代码:

示例:

public void onListItemClick(ListView parent, View view, int position, long id) {

         b = new AlertDialog.Builder(this);

        b.setMessage("Are you sure?");
        b.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

              yes();
                }
        });
        b.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                  no();
                }
        });
        b.show();
    Toast.makeText(this, "no", Toast.LENGTH_LONG).show();
    }

   public void yes()
   {
       Toast.makeText(this, "yes", Toast.LENGTH_LONG).show();
   }
   public void no()
   {
       Toast.makeText(this, "no", Toast.LENGTH_LONG).show();
   }

Call two separate functions in positivebutton and in negativebutton, and write the code what you want:

sample:

public void onListItemClick(ListView parent, View view, int position, long id) {

         b = new AlertDialog.Builder(this);

        b.setMessage("Are you sure?");
        b.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

              yes();
                }
        });
        b.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                  no();
                }
        });
        b.show();
    Toast.makeText(this, "no", Toast.LENGTH_LONG).show();
    }

   public void yes()
   {
       Toast.makeText(this, "yes", Toast.LENGTH_LONG).show();
   }
   public void no()
   {
       Toast.makeText(this, "no", Toast.LENGTH_LONG).show();
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文