IF condition is true but does not execute related code [duplicate]

3

I have my program that displays a message and in the same message the user must enter a code. I'm trying to do the validation if he typed something or not, for that I use the code:

AlertDialog.Builder alert = new AlertDialog.Builder(context);
    alert.setTitle("Código final"); //Set Alert dialog title here
    alert.setMessage("Insira o código fornecido pela Nobre de la Torre:"); //Message here

    // Set an EditText view to get user input 
    final EditText input = new EditText(context);
    alert.setView(input);

    alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
     //Ações do botão da mensagem
     String srt = String.valueOf(input.getEditableText());
     if(srt!=""){ //ESSE É O IF QUE ESTÁ COM ERRO
     Toast.makeText(context,srt,Toast.LENGTH_LONG).show();  
     }
     else
     {
     Toast.makeText(context,"Campo vazio!",Toast.LENGTH_LONG).show();  
     }  
      String hexNumber = srt;
      int decimal = Integer.parseInt(hexNumber, 16);
      System.out.println("Hex value is " + decimal); 
                 [COntinua...]

Even with the variable String srt being "" it sees as different from "" and executes the code below it

String srt = String.valueOf(input.getEditableText());
if(srt!=""){ 
    Toast.makeText(context,srt,Toast.LENGTH_LONG).show();
}

Being that he should run else :

else {
    Toast.makeText(context,"Campo vazio!",Toast.LENGTH_LONG).show();}
    
asked by anonymous 20.05.2015 / 19:29

1 answer

3

Modify for this implementation

String srt =input.getText().toString();
if(!srt.equals(""))
The method getEditableText() of class EditText returns an object of type Editable , to get the text you must use the getText() method that returns an object of type CharSequence , soon after it simply converts it for String.

    
20.05.2015 / 19:42