while loop login
I've completed my login page. My problem now is I can only log in using the first account created. I found out that I have not complete my login page. The missing item is the while..loop code so that the application will check the other existing user instead of only looking for a match for the first user.
if(username.equals(c.getString(1)))
{
if(password.equals(c.getString(2)))
{
Context context = getApplicationContext();
int duration = Toas开发者_StackOverflow中文版t.LENGTH_LONG;
Toast toast = Toast.makeText(context, "LOGIN SUCCESS", duration);
toast.show();
Intent intent=new Intent(Login.this,Test.class);
startActivity(intent);
}
I do know that the code is something like while (c.moveToNext())
but I do not know how to apply it on my if..else statement.
I hope you can help me out on this simple task. Thank you!
Instead of looping through all users to find the right one you should load the user associated with the given username and match the password against his.
It makes no sense to match any other user than the one the user of your application has entered.
EDIT: Based on your comment it could be like this:
//make sure the user input is tested against SQL-injections etc.
Cursor c = db.rawQuery("SELECT username FROM Users WHERE username='?' AND password='?'",
new String[] {username, password});
if(c.moveToFirst()) {
// at least one row was returned, this should signal success
Context context = getApplicationContext();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "LOGIN SUCCESS", duration);
toast.show();
Intent intent=new Intent(Login.this,Test.class);
startActivity(intent);
} else {
// authentication failed, handle error (message etc)
}
精彩评论