0

I created an android app project in android eclipse, I have a problem with the log-in command. i have already registered but i can't login, the username and password is already registered in the database using sqlite browser. But when I put the username and password I cant login. I dont know where the error is. please help.

here's my mainactivity.java code:

                package com.example.howtos;



            import com.example.howtos.DatabaseManager;
                        import com.example.howtos.MainActivity;
                                    import com.example.howtos.R;
                                    import com.example.howtos.reg;

                                    import android.os.Bundle;
                                    import android.app.Activity;
                                    import android.content.Intent;
                                    import android.database.Cursor;
                                    import android.database.CursorIndexOutOfBoundsException;
                                    import android.view.View;
                                    import android.widget.Button;
                                    import android.widget.EditText;
                                    import android.widget.TextView;
                                    import android.widget.Toast;


                                    public class MainActivity extends Activity {

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

                                            final DatabaseManager dataBase;
                                            dataBase = DatabaseManager.instance();

                                            Button b = (Button) findViewById (R.id.btn12);
                                            b.setOnClickListener(new View.OnClickListener() {
                                                @Override    public void onClick(View v) {
            try
            {
            final EditText username = (EditText) findViewById(R.id.editText1);
            final EditText password = (EditText) findViewById(R.id.editText2);

            final String COLUMN_PW = "password";

            Cursor cursor = dataBase.select("SELECT password FROM users WHERE username = '" + username.getText().toString() + "';");
            String s = cursor.getString(cursor.getColumnIndex(COLUMN_PW));

            if(password.getText().toString() == s)
            {
                            startActivity(new Intent(MainActivity.this,homepage.class)); 


                Toast.makeText(getApplicationContext(), "Successfully Login", Toast.LENGTH_SHORT).show();
                cursor.close();
                }                               
                }
                catch(CursorIndexOutOfBoundsException e1)
                {
                Toast.makeText(getApplicationContext(), "Invalid Username or Password", Toast.LENGTH_SHORT).show();
                }       


                                                }
                                            });

            TextView r = (TextView) findViewById (R.id.textView7);
            r.setOnClickListener(new View.OnClickListener() {
            @Override    public void onClick(View v) {
            startActivity(new Intent(MainActivity.this,reg.class)); 
            }
            });
         }



                                    }

1 Answers1

0

The issue is most likely with this line:

if(password.getText().toString() == s)

which should become:

if(password.getText().toString().equals(s))

In Java, using '==' always compares object references and although two strings may contain the same value, they are different objects hence your comparison always fails. Hence you should use the 'equals' method on string which will compare the string values. A comprehensive explanation can be found here:

How do I compare strings in Java?

Edit:

A second issue is that you are getting a CursorIndexOutOfBoundsException (because the catch block for that is where your invalid password message is delivered from). Having opened your cursor, you need to move it to the first row before trying to access data from it:

cursor.MoveToFirst();

Also you will need to display the error message when the password/username is wrong. For example:

try
{
    final EditText username = (EditText) findViewById(R.id.editText1);
    final EditText password = (EditText) findViewById(R.id.editText2);

    final String COLUMN_PW = "password";

    Cursor cursor = dataBase.select("SELECT password FROM users WHERE username = '" + username.getText().toString() + "';");
    cursor.MoveToFirst();
    String s = cursor.getString(cursor.getColumnIndex(COLUMN_PW));

    if(password.getText().toString().equals(s))
    {
        startActivity(new Intent(MainActivity.this,homepage.class)); 
        Toast.makeText(getApplicationContext(), "Successfully Login", Toast.LENGTH_SHORT).show();
        cursor.close();
    }                               
    else
    {
        Toast.makeText(getApplicationContext(), "Invalid Username or Password", Toast.LENGTH_SHORT).show();
    }
}
catch(CursorIndexOutOfBoundsException e1)
{
    Toast.makeText(getApplicationContext(), "Error accessing database", Toast.LENGTH_SHORT).show();
}

Also remember to check that your cursor actually returned any data before reading from it (which can happen here if the username does not exist). You can do that by checking the value of cursor.getCount()

Community
  • 1
  • 1
NigelK
  • 8,255
  • 2
  • 30
  • 28
  • thank you for reply, but still didn't work. it's just saved in the database, but when i click the login button it says "invalid username or password." is there any other options? thank you! – John Estilo Oct 15 '14 at 19:13
  • There is a 2nd issue which I have explained in my edit above. – NigelK Oct 15 '14 at 19:53
  • It works! Thank you very much @NigelK i will study more what did you said – John Estilo Oct 15 '14 at 20:18