If Loop does not run after closing the dialog box (the first time with java) Java / Android

advertisements

I'm trying to change a the text in a textview after this dialog box is closed by pressing the "ok_button". But I wanted to do it in a loop. I set "alleroclosed" as boolean and initialized it as false. After I press the "ok_button" the loop will not initiate, or it won't take my boolean variable "alleroclosed"

{
     Button ok_button = (Button) dialog.findViewById(R.id.Ok_button);
     ok_button.setOnClickListener(
         new OnClickListener()
         {
             @Override
             public void onClick(View v)
             {
                 alleroclosed=true;
                 dialog.dismiss();
             }
         });

     if(alleroclosed)
     {
         alleroclosed=false;
         alleroSTATUS.setText("it works");
     }
}

======== It won't change the string to "it works"

If you can help me in any way I'd really appreciate it. I'm a true novice at this. Thanks!


I think that problem is that you are calling

if (alleroclosed) {
  alleroclosed=false;
  alleroSTATUS.setText("it works");
}

outside of onClick() method and for this reason it never be applied.

Your block of code is directly placed in onCreate() method and this method is not called when click event is catched so it is applied only once - when onCreate() is called with alleroclosed = false.

So it will work when you'll place it inside of onClick() method.

...
public void onClick(View v) {
   alleroclosed = true;
   dialog.dismiss();
   if (alleroclosed) {
      alleroclosed=false;
      alleroSTATUS.setText("it works");
   }
...

}