I am developing an android app on android studio & Firebase. There are 4 different types of users (student, tutor, parent, admin). I want to do it with if statements something like this:
if (user.profile.userstatus == "Student")
intent studentactivity
if (user.profile.userstatus == "Tutor")
intent tutoractivity
So, my main question is that how can I bring the data from firebase?
And, this is my code for distinguishing:
if (isRegistering) {
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
showProgress(false);
if (!task.isSuccessful()) {
Toast.makeText(LoginActivity.this, "Could Not Register", Toast.LENGTH_SHORT).show();
} else {
// show username diaglog
UsernameDialogFragment dialog = new UsernameDialogFragment();
dialog.show(getFragmentManager(),null);
}
}
});
} else {
Task<AuthResult> authResultTask = mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
showProgress(false);
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
FirebaseUser user = mAuth.getCurrentUser();
//student login
if (get idstatus == "Student")
Intent studentintent = new Intent(getBaseContext(), studentActivity.class);
startActivity(studentintent);
//admin login
else if (get idstatus == "Admin")
Intent adminintent = new Intent(getBaseContext(), AdminActivity.class);
startActivity(adminintent);
//parent login
else if (get idstatus == "Parent")
Intent parentintent = new Intent(getBaseContext(), ParentActivity.class);
startActivity(parentintent);
//tutor login
else {
Intent tutorintent = new Intent(getBaseContext(), TutorActivity.class);
startActivity(tutorintent);
}
} else {
// If sign in fails, display a message to the user.
Toast.makeText(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
And this is code for storing data(now working):
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
//https://myapplication4-124da.firebaseio.com/ https://console.firebase.google.com/project/myapplication4-124da/database/myapplication4-124da/data/
EditText usernameField = ((AlertDialog) dialog).findViewById(R.id.username);
EditText firstnameField = ((AlertDialog) dialog).findViewById(R.id.firstname);
EditText lastnameField = ((AlertDialog) dialog).findViewById(R.id.lastname);
String username = usernameField.getText().toString();
String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
String firstname = firstnameField.getText().toString();
String lastname = lastnameField.getText().toString();
User aUser = new User(username, firstname, lastname, choice, 0.0);
FirebaseDatabase.getInstance().getReference("users").child(userId).child("profile").setValue(aUser);
Intent intent = new Intent(getActivity().getBaseContext(), studentActivity.class);
startActivity(intent);