0

UI from using the code i mention I have my database set up like this:

root

-Students
    -user1
        -name
        -pass
        -etc...
    -user2

-Admin
    -admin1

I want to check if the current user is an admin or a student so that i can go to a certain reference in the database

This is my code:

    user = FirebaseAuth.getInstance().getCurrentUser();
    reff = FirebaseDatabase.getInstance().getReference().child("Students").child(user.getUid());

    reff.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            name = dataSnapshot.child("name").getValue().toString();
            lastName = dataSnapshot.child("lastName").getValue().toString();
            studentID = dataSnapshot.child("studentID").getValue().toString();
            email = dataSnapshot.child("email").getValue().toString();
            password = dataSnapshot.child("password").getValue().toString();
            confirmPassword = dataSnapshot.child("password").getValue().toString();
            inputStudentID.getEditText().setText(studentID);
            inputName.getEditText().setText(name);
            inputLastName.getEditText().setText(lastName);
            inputEmail.getEditText().setText(email);
            inputPassword.getEditText().setText(password);
            inputConfirmPassword.getEditText().setText(confirmPassword);


        }

to get the students info I have to do

FirebaseDatabase.getInstance().getReference().child("Students").child(user.getUid());

but if I were to log on as an admin how would I go to that branch and display their info. I've already tried using an if statement with

dataSnapshot.child("Students").hasChild(user.getUID)

with the reference of

FirebaseDatabase.getInstance().getReference()

to see if the user logged in is in the student branch but that didn't work.

UI for both admin and student with the code in picture 3

Code for image 2

Spencer
  • 11
  • 6

1 Answers1

0

In order to see if a child exists in the branch of a node and run your code based on that, you have to use a call back function.

See this article for reference

Spencer
  • 11
  • 6