简体   繁体   中英

Android login button not working

I am making an android app that simply allows the user to create an account and use that account name and password, should the account exist, to login to a page. I believe my program has worked right up to the point of login. the method actually runs but the if-else has not properly executed. Any help is appreciated, thank you.

Login method:

ArrayList<User> users = new ArrayList<>(); // already contains info

public void login(View view){ 
     // EditText where user enters the username
    EditText userText = (EditText) view.findViewById(R.id.username);
    // EditText where user enters the password
    EditText passText = (EditText) view.findViewById(R.id.password);

    for (int i = 0;i<users.size();i++) {
        String p = passText.getText().toString(); //password string
        String n = userText.getText().toString(); //username string

         // checks if names of the are equal
        boolean name = (n.equals(users.get(i).getName()));
         // checks if passwords of the are equal
        boolean pass = (p.equals(users.get(i).getPassword()));
        if (name && pass) {
            Toast toast = Toast.makeText(getApplicationContext(), "Login Successful!", Toast.LENGTH_LONG);
            toast.show();
            intent = new Intent(MainActivity.this, EnteredPage.class);
            startActivity(intent);
            break;
        }
        if (i == users.size() - 1) { //if this is the last User object         

                Toast toast = Toast.makeText(getApplicationContext(), "User does not exist!", Toast.LENGTH_LONG);
                toast.show();
        }
    }
 }

The problem is the first line. ArrayList<User> users = new ArrayList<>(); When you say this, the arraylist becomes reset and contains no values. All the usernames and password are deleted. You can store the username and password on a sqlite database. And then while doing login, check whether the user exists in the database. No need to retrieve an arraylist and compare with its values.

Recommendation: user authentication should be done through a server and not locally on the app

ArrayList<User> users; // already contains info
private String userName;
private String pass;
private EditText userText;
private EditText passText;
private String inputUser;
private String inputPass;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
users = new ArrayList<>();
  for(User user: users){
        userName = user.getName();
        pass = user.getPassword()'
    }
    // EditText where user enters the username
userText = (EditText) findViewById(R.id.username);
// EditText where user enters the password
passText = (EditText) findViewById(R.id.password);
}
public void login(View view){ 
    inputPass = passText.getText().toString(); //password string
    inputUser = userText.getText().toString(); //username string


    if (inputPass.equals(pass) && (inputUser.equals(userName))) {
        Toast toast = Toast.makeText(getApplicationContext(), "Login Successful!", Toast.LENGTH_LONG);
        toast.show();
        intent = new Intent(MainActivity.this, EnteredPage.class);
        startActivity(intent);
        break;
    }
    if (i == users.size() - 1) { //if this is the last User object         

            Toast toast = Toast.makeText(getApplicationContext(), "User does not exist!", Toast.LENGTH_LONG);
            toast.show();
    }
}
}

Try it!

I would suggest you to check the data in your variables by debugging the code or using log. That way you might be able to pin-point to the exact cause whether the data you are looking for is actually present or not.

Also you might need to change .equals to .equalsIgnoreCase for username. The case sensitive data will be ignored in that case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM