How to take an object from a class to another activity?

I have a login app that has to have a user class with the user information in an object. But now I'm trying to figure out how I could compare the user object with the username and password the user inputs to see if it is correct. So I need to be able to see for example if the user inserts "Jason" into the username txt and "Sword" into the password txt I need to see if it is correct and matches the one in the user class. Does anyone know how I would go about doing so? Any help would be muchly appreciated I'm just really stuck.

public class User {

    String userName;
    String password;
    String favColor;

    public User(String userName, String password, String favColor){
        this.userName = userName;
        this.password = password;
        this.favColor = favColor;

    }
    public class UserGroup{
        User user1 = new User ("Jason", "Sword", "Red");
        User user2 = new User ("Billy", "Dinosaur", "Blue");
        User user3 = new User ("Zack", "Elephant", "Black");
        User user4 = new User ("Trini", "Tiger", "Yellow");
        User user5 = new User ("Kimberly", "Bird", "Pink");
        
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button logInBt = findViewById(R.id.buttonLogIn);
    final EditText userEditText = findViewById(R.id.editTextTextPersonName);
    final EditText passwordEditText = findViewById(R.id.editTextTextPassword);



    logInBt.setOnClickListener(new View.OnClickListener() {
        public void  onClick(View view) {

            String userET = userEditText.getText().toString();
            String userPassword = passwordEditText.getText().toString();



        }
    });

1 answer

  • answered 2020-09-14 06:51 Sideeg MoHammed

    you have to make the user Class filed static to reached it any where

    or you can use instate of it and i will use instate to do not change your code

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    final Button logInBt = findViewById(R.id.buttonLogIn);
    final EditText userEditText = findViewById(R.id.editTextTextPersonName);
    final EditText passwordEditText = findViewById(R.id.editTextTextPassword);
    
    
    
    logInBt.setOnClickListener(new View.OnClickListener() {
        public void  onClick(View view) {
    
            String userET = userEditText.getText().toString();
            String userPassword = passwordEditText.getText().toString();
    UserGroup users = new UserGroup();
    if (userEt.equals(user1.userName)&& userPassword.equals(user1.password ){
    // write here what you want user 1 to do after login
     }else if (userEt.equals(user2.userName)&& userPassword.equals(user2.password ){
       // write here what you want user 2 to do after login
     }
    
    
    
        }
    });
    

    do else if to check all the user and you can change it to a list of users to make it easer