1

Edit: Can you guys teach me how to add a user class in the package? i don't know how

Hello im a total beginner on this but can i ask why the writenewuser User user = new user says cannot resolve symbol user? I plan to like add the login details on the database and then set a value on them either 1 or 2 so that i can choose on what dashboard the registered user would like to go. But i'm puzzled since when I'm searching for guides or samples every time I go with the public void writenewuser. The "User user = new user" code will always be an error saying "Cannot resolve symbol User"

public class signup extends AppCompatActivity {
private EditText inputEmail, inputPassword, inputName;
private Button btnSignIn, btnSignUp, btnResetPassword;
private ProgressBar progressBar;
private FirebaseAuth auth;
private DatabaseReference mFirebaseDatabase;
private FirebaseDatabase mFirebaseInstance;
private String userId;
private int type =1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_signup);
    // Displaying toolbar icon
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setIcon(R.mipmap.ic_launcher);
    //Get Firebase auth instance
    auth = FirebaseAuth.getInstance();
    btnSignIn = (Button) findViewById(R.id.sign_in_button);
    btnSignUp = (Button) findViewById(R.id.sign_up_button);
    inputEmail = (EditText) findViewById(R.id.email);
    inputPassword = (EditText) findViewById(R.id.password);
    inputName = (EditText) findViewById(R.id.name);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    btnResetPassword = (Button) findViewById(R.id.btn_reset_password);

    mFirebaseInstance = FirebaseDatabase.getInstance();
    // get reference to 'users' node
    mFirebaseDatabase = mFirebaseInstance.getReference("Users");

    btnResetPassword.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(signup.this, ResetPasswordActivity.class));
        }
    });
    btnSignIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
    btnSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String email = inputEmail.getText().toString().trim();
            String password = inputPassword.getText().toString().trim();
            final String name = inputName.getText().toString().trim();
            if (TextUtils.isEmpty(email)) {
                Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(password)) {
                Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
                return;
            }
            if (password.length() < 6) {
                Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
                return;
            }
            progressBar.setVisibility(View.VISIBLE);
            //create user
            auth.createUserWithEmailAndPassword(email, password)
                    .addOnCompleteListener(signup.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            Toast.makeText(signup.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
                            progressBar.setVisibility(View.GONE);
                            // If sign in fails, display a message to the user. If sign in succeeds
                            // the auth state listener will be notified and logic to handle the
                            // signed in user can be handled in the listener.
                            if (!task.isSuccessful()) {
                                Toast.makeText(signup.this, "Authentication failed." + task.getException(),
                                        Toast.LENGTH_SHORT).show();
                            } else {
                                finish();
                                onAuthSuccess(task.getResult().getUser());
                                String user_id = auth.getCurrentUser().getUid();
                                DatabaseReference user_details =  mFirebaseDatabase.child(user_id);
                                user_details.child("name").setValue(name);
                                user_details.child("type").setValue("1");
                                Intent mainIntent = new Intent(signup.this, LoginActivity.class);
                                mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(mainIntent);

                            }
                        }

                    });


        }
        private void onAuthSuccess(FirebaseUser user) {
            String username = usernameFromEmail(user.getEmail());

            // Write new user
            writeNewUser(user.getUid(), username, user.getEmail());

            // Go to MainActivity
            startActivity(new Intent(signup.this, LoginActivity.class));
            finish();
        }
        private String usernameFromEmail(String email) {
            if (email.contains("@")) {
                return email.split("@")[0];
            } else {
                return email;
            }
        }
        private void writeNewUser(String userId, String name, String email) {
            User user = new User(name, email);

            mFirebaseDatabase.child("Users").child(userId).setValue(user);
        }

    });



}

@Override
protected void onResume() {
    super.onResume();
    progressBar.setVisibility(View.GONE);
}

}

Yamiess
  • 251
  • 4
  • 15

1 Answers1

2

You need to create the class User like this example:

public class User{
private String name,email;

public User(){

 }

public User(String name, String email){
this.name=name;
this.email=email;
  }
   public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email=email;
}

}
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • Ohhhh, Can i ask why some of the samples i checked didn't have that? or they have it but stored it somewhere? – Yamiess Feb 10 '18 at 09:56
  • which samples are u checking? Also this is a POJO class you can create to organize storing data in the database or you can store each one by its own – Peter Haddad Feb 10 '18 at 09:57
  • your properties in the pojo class need to be the same as in your firebase database, example if it is "name" in pojo then in firebase database should be "name" no capital letter.. – Peter Haddad Feb 10 '18 at 09:59
  • Hello there can i ask what is a pojo class and the difference? my plan is to like it would create a database when registering users – Yamiess Feb 10 '18 at 10:13
  • @Yamiess check this https://stackoverflow.com/questions/14172621/whats-the-advantage-of-pojo. Yes I understand for that you need to use also the firebase database to store data there https://www.learnhowtoprogram.com/android/data-persistence/firebase-writing-pojos – Peter Haddad Feb 10 '18 at 10:16