【问题标题】:Android: using AlertDialog when an item from a listview is long pressedAndroid:长按列表视图中的项目时使用 AlertDialog
【发布时间】:2011-07-20 14:46:16
【问题描述】:

我有一个由列表视图创建的项目列表。我想长按列表中的一项并打开一个警报对话框,并根据该对话框上的是或否键来设置全局变量。我正在使用的代码位于“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”都不会改变。 有人可以告诉我代码有什么问题吗?

感谢您的帮助。

【问题讨论】:

  • 放一个 Log.i("SOMETHING", "ENVOKED");在每个侦听器中查看它是否在单击按钮时被激发?为什么不使用布尔值? :) True for Yes false for No..它更便宜。

标签: android android-alertdialog long-click


【解决方案1】:

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

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

所以yesOrNo变量的值就是它的初始值。

解决方案

您可以在positiveButton的onClick()方法中调用doSomething(0),在negativeButton的onClick()方法中调用doSomething(1)

【讨论】:

  • 我怎样才能实现我想要做的呢?感谢您的帮助。
【解决方案2】:

yesOrNo 正在改变。但你无法捕捉到它。因为 AlertDialog 是异步的,它不会等待点击。它会执行范围的其余部分。如果你想查看更改,请查看点击时的值在对话框按钮上。然后你会看到

【讨论】:

  • 关于如何实现我想要做的任何建议?在执行其余操作之前,我想等待并得到是或否的答案。感谢您的帮助。
  • 把剩下的放在另一个方法里,点击后调用
【解决方案3】:

您不能在调用 b.show() 后立即检查 yesOrNo 的值。仅仅因为现在显示了对话框,并不意味着单击了按钮。您应该在 OnClickListener 内执行 DO SOMETHING 或从 OnClickListener 内调用方法。

【讨论】:

    【解决方案4】:

    下面的测试不是在正确的地方:

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

    它会在您的对话框创建后进行评估,而不是在用户单击按钮后进行评估。所以当时yesOrNo还是false,而我们从来没有DO SEOMTHING

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

    【讨论】:

      【解决方案5】:

      在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();
         }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多