0

I came a cross this problem: I can't get EditText (int) value, turn it in to a string and then use it in Alert Dialog.

(I am making a Phone number Verification. Not an advanced one! Just simple that confirms the new user real.) Here is my code:

sms_verification:

public class sms_verification extends AppCompatActivity {
    ImageButton send;
    EditText text;
    String myEditValue;
    public  static int phone;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sms_verification);

        send = (ImageButton)findViewById(R.id.imageButton);
        text = (EditText)findViewById(R.id.editText);       
    }

    public void AlertDialog(View view) {    
        myEditValue = text.getText().toString();
        phone = Integer.parseInt(myEditValue);   

        AlertDialog.Builder alertdlg = new AlertDialog.Builder(this);
        alertdlg.setMessage("Confirmation")
                .setCancelable(false)
                .setMessage("Is " + phone +" your phone number?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        setContentView(R.layout.sms_verification2);
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

        AlertDialog alertDialog = alertdlg.create();
        alertDialog.show();    
    }
}
ManoDestra
  • 5,756
  • 6
  • 22
  • 48
Onhar
  • 49
  • 1
  • 7

3 Answers3

0

Sorry, I can't comment yet. But does your phone number have dashes in it? Looking at your dialog, Why do you need to parse the integer values out anyway?

.setMessage("Is " + myEditValue +" your phone number?")
ChoklatStu
  • 209
  • 1
  • 9
0

First you are converting String to int , then again int to String , this make no sense.

Just do this

"Is " + text.getText().toString() +" your phone number?"
Vishwesh Jainkuniya
  • 2,561
  • 3
  • 15
  • 33
-2

You should be able to use:

String.valueOf(phone)

e.g.

.SetMessage("Is " + String.valueOf(phone) + " your phone number")
Sparky
  • 21
  • 3
  • Why will `valueOf` work when `parseInt` doesn't ? `Actually, valueOf uses parseInt internally. The difference is parseInt returns an int primitive while valueOf returns an Integer object`. [Source](http://stackoverflow.com/questions/7355024/integer-valueof-vs-integer-parseint) – UDKOX May 19 '16 at 15:09
  • java will auto convert values to string if they're being concatenated, this `String.valueOf()` will not help – Tim May 19 '16 at 15:09