-2

I'm very new to android , this project involves Registration as the first step which takes the username and password from the user and the next activity requires a login inorder to proceed. The login is required to validate the username and password. I am unable to get the Username which is the first field in the table and the last field password.

log.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    un = user.getText().toString();
    pw = pass.getText().toString();

    Record rec = new Record(MainActivity.this);
    SQLiteDatabase sqldb = rec.getReadableDatabase();
    Cursor c = sqldb.query(Record.tbname, null, 
            null, null, null, null, null);
    while(c.moveToNext())
    {
        username = c.getString(0);
        password = c.getString(3);
    }

      if(un == username && pw == password)
      {
        Toast.makeText(MainActivity.this, "Valid Credentials", Toast.LENGTH_LONG).show();

        Intent next = new Intent();
        next.setClass(MainActivity.this, HomeActivity.class);
        startActivity(next);
      }
      else
      {
          Toast.makeText(MainActivity.this, "Invalid Credentials", Toast.LENGTH_LONG).show();
      }  
  }
});

I'm cannot fetch values in order to validate the login details.

Gal Dreiman
  • 3,969
  • 2
  • 21
  • 40
TejanD
  • 3
  • 1
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – earthw0rmjim Sep 04 '16 at 09:57

2 Answers2

0

Dont use == for comparing string values use equalsIgnoreCase()

Use equals() if you dont want to ignore case sensitivity.

if(un == username && pw == password)

to

if(un.equalsIgnoreCase(username) && pw.equalsIgnoreCase(password))
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
0

The == operator is only for primitive datatypes. For String comparison you can use equals() or equalsIgnoreCase().

Since you are comparing username and password it is highly recommended to use equals().

                 if(un.equals(username) && pw.equals(password))
                  {
                    Toast.makeText(MainActivity.this, "Valid Credentials", Toast.LENGTH_LONG).show();

                    Intent next = new Intent();
                    next.setClass(MainActivity.this, HomeActivity.class);
                    startActivity(next);
                  }
                  else
                  {
                      Toast.makeText(MainActivity.this, "Invalid Credentials", Toast.LENGTH_LONG).show();
                  }  
Umang
  • 966
  • 2
  • 7
  • 17