user1143989
user1143989

Reputation: 163

Android Dialog return String?

Hi I am popping up dialog to take comments from user. And returning a value according to that. That "rcomment" is a global variable. And it returns null. This is not working. What am I doing wrong ?

 public String getDoNotBoardDialog(final int groupposition)
     {
         final Dialog dia = new Dialog(this);
         dia.requestWindowFeature(Window.FEATURE_NO_TITLE); 
         dia.setContentView(R.layout.donotboard);
         final EditText donotedit = (EditText) dia.findViewById(R.id.donotboardcomment);
         donotedit.setText(""); 

         Button button1 = (Button) dia.findViewById(R.id.donotboardbutton);
         button1.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 r =  donotedit.getText().toString();
                 String boardingComment = getString(R.string.donotboard) + " " + r;
                 PostCommentForAC(groupposition, boardingComment);
                 Intent intent = new Intent(getBaseContext(), TestExList.class); 
                 intent.putExtra("EmpID", empid);
                 startActivity(intent);
                 rcomment = "true";
                 dia.cancel();
        }
       });
          Button button2 = (Button) dia.findViewById(R.id.boardbutton);
          button2.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                rcomment = "false";
            dia.cancel();
        }
       });
      dia.show();
    return rcomment;
     }

Upvotes: 0

Views: 572

Answers (4)

zapl
zapl

Reputation: 63955

Edit: dont use below code. It will kill your app (ANR).

You would have to wait before you can return something. A quick (but really dirty) solution would be to add some wait/notify mechanism like so: (written blind so might contain some errors).

public String getDoNotBoardDialog(final int groupposition) {
     // some Object to wait on
     final Object waitOnMe = new Object();
     final Dialog dia = new Dialog(this);
     dia.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dia.setContentView(R.layout.donotboard);
     final EditText donotedit = (EditText) dia.findViewById(R.id.donotboardcomment);
     donotedit.setText(""); 

     Button button1 = (Button) dia.findViewById(R.id.donotboardbutton);
     button1.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
             r =  donotedit.getText().toString();
             String boardingComment = getString(R.string.donotboard) + " " + r;
             PostCommentForAC(groupposition, boardingComment);
             Intent intent = new Intent(getBaseContext(), TestExList.class); 
             intent.putExtra("EmpID", empid);
             startActivity(intent);
             rcomment = "true";
             dia.cancel();
            // stop waiting.
            synchronized(waitOnMe) {
                waitOnMe.notify();
            }
    }
   });
      Button button2 = (Button) dia.findViewById(R.id.boardbutton);
      button2.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
            rcomment = "false";
        dia.cancel();
        // stop waiting.
        synchronized(waitOnMe) {
            waitOnMe.notify();
        }
    }
   });
  dia.show();
  // this wait's until someone calls notify
  synchronized (waitOnMe) {
      try {
          waitOnMe.wait();
      } catch (InterruptedException e) {}
  }
return rcomment;
 }

It's problematic though. You could miss the notify() and thus never stop waiting (e.g. when you close the dialog via the "back" button). A much cleaner and safer solution would be to use some Callback mechanism (you call some method in your program from each onClick) to get a value from the dialog.

Upvotes: 0

wesdfgfgd
wesdfgfgd

Reputation: 693

Try using this string.equals(data) it should find out if the string is the same. Since rcomment is a string. Also like Soham said it will not update only until it is clicked.

I suggest that in the future you should change to Boolean rcomment. Since it only looks like you are doing either true or false status.

Upvotes: 0

Soham
Soham

Reputation: 4970

In you application

the program flows to

return rcomment;

directly after going to

dia.show();

Remember it does not wait for the button to be clicked before it goes to the return statement!!! It goes to return statement directly after the dialog is shown (before the button is clicked)

Upvotes: 0

Peter Ajtai
Peter Ajtai

Reputation: 57715

The getDoNotBoardDialog will initial return rcomment as null. rcomment will only be changed to "true" or "false" when the onClickListeners are fired. They are fired not when getDoNotBoardDialog is run, but after that, whenever the onClickListeners are fired.

Whatever you want to happen when rcomment is changed to "true" or "false" should be placed in the onClick methods. So if you want to check what rcomment is after a user clicks, do it there.

Upvotes: 1

Related Questions