Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get String from EditText

login.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            if( username_text.getText().toString() == "admin" &&
                    pass_text.getText().toString() == "password"){

                startActivityForResult(i, ACTIVITY_CREATE);

            }else{
                Toast.makeText(gps_gui.this, "Your username or password is not correct! Please, insert again",
                        Toast.LENGTH_SHORT).show();
                clearEditText();
            }
        }

    });

I'm try to check it, I found it not go to if condition.

like image 638
Yoo Avatar asked Jan 30 '11 05:01

Yoo


2 Answers

When using java, you must compare Strings using the String.equals(String) method. The == comparison checks to see if the String object values are equal, which undoubtedly they are not. Try to change you example to :

"admin".equals(username_text.getText().toString()) &&"password".equals(pass_text.getText().toString())

Its also smart to put the static string first, in case the string value of the value being checked is null.

like image 60
Nick Campion Avatar answered Sep 29 '22 12:09

Nick Campion


Use String class's equals() method to compare Strings. The following links would give you more details.

About equals(Object yourObj) method (CASE sensitive)

equalsIgnoreCase(String yourStringObject) can be used, if you want it to be not case sensitive!

like image 43
msumaithri Avatar answered Sep 29 '22 12:09

msumaithri