有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何检查数据库中的a分支中是否存在子级,以便基于该子级返回值

UI from using the code i mention 我的数据库设置如下:

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

-Admin
    -admin1

我想检查当前用户是管理员还是学生,以便我可以访问数据库中的某个引用

这是我的代码:

    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);


        }

为了得到学生们的信息,我必须

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

但如果我以管理员的身份登录,我将如何前往该分支机构并显示他们的信息。我已经试过用if语句

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

参考

FirebaseDatabase.getInstance().getReference()

查看登录的用户是否在学生分支中,但这不起作用

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

Code for image 2


共 (1) 个答案

  1. # 1 楼答案

    为了查看节点的分支中是否存在子节点,并在此基础上运行代码,必须使用回调函数

    参考this文章