英文:
How do I check if a child exists in the a branch in the database so that I can return values based on that child
问题
// 获取当前用户
user = FirebaseAuth.getInstance().getCurrentUser();
// 获取指向当前用户在 "Students" 分支下的引用
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);
}
});
// 如果以管理员身份登录,如何获取管理员分支并显示信息
// 使用 "Admin" 分支
adminReff = FirebaseDatabase.getInstance().getReference().child("Admin").child(user.getUid());
adminReff.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// 获取管理员信息并显示
// ...
}
});
请注意,这里的代码只是一个翻译版本,并未对原始代码逻辑进行修改。如果你有进一步的问题或需要代码方面的帮助,请随时提问。
英文:
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.
答案1
得分: 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论