0

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.

TJ1
  • 7,578
  • 19
  • 76
  • 119
  • Put one Log.i("SOMETHING", "ENVOKED"); inside every listener to see if it envoked when the button is clicked? Why dont you use booleans? :) True for Yes false for No..its cheaper. – Nikola Despotoski Jul 20 '11 at 14:49

5 Answers5

2

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.

Onuray Sahin
  • 4,093
  • 4
  • 33
  • 34
0

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

Rasel
  • 15,499
  • 6
  • 40
  • 50
  • Any suggestion on how to achieve what I want do do then? I want to wait and get the answer to yes or no before executing the rest. Thanks for the help. – TJ1 Jul 20 '11 at 14:55
  • just put the rest in another method and call after click – Rasel Jul 20 '11 at 14:58
0

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.

Amandeep Grewal
  • 1,801
  • 3
  • 19
  • 30
0

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.

Shlublu
  • 10,917
  • 4
  • 51
  • 70
0

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();
   }
Abhi
  • 8,935
  • 7
  • 37
  • 60