java - 如何从 Firebase 数据库中的子项检索特定数据
问题描述
我是 Firebase 数据库的新手,我在 Firebase 中的数据结构是这样的:
Root- users-sec_a, sec_b, sec_c
在每个部分(sec_a,sec_b,sec_c)中都会有用户的 uid。在每个部分中至少有 40 个 uid。并且在所有 uid 中都有一个公共数据子项(姓名、rollno、出席)现在我的问题是我只想显示其中一个部分中存在的所有 uid 的名称和 rollno(要显示的部分是由用户输入的)。
我怎样才能得到这些数据?
解决方案
这很容易。
String sectionSelectedByUser = "sec_a"; // For example
现在获得firebase数据库的引用
if (FirebaseAuth.getInstance().getCurrentUser() != null){ // if you need user to be signed in..
FirebaseDatabase.getInstance().getReference().child("users").child(sectionSelectedByUser).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) { // iterates through all your UID in this section
if (childSnapshot.hasChild("name")){ // if current uid has name then fetch it
String name = childSnapshot.child("name").getValue().toString();
}
if (childSnapshot.hasChild("rollno")){ // if current UID has rollno then fetch it
String rollno = childSnapshot.child("rollno").getValue().toString();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
推荐阅读
- unix - 如何将两个输入通过管道传输到同一个命令?
- openimageio - OpenImageIO:以显示窗口分辨率而不是数据分辨率打开图像
- python - 缩放pygame屏幕截图
- node.js - 来自另一个 js 文件的函数不执行任何操作
- java - Java 无法解析为类型
- amazon-web-services - 使用 Cognito 用户池限制/限制 API 网关端点上用户的最佳方法?
- macos - 如何在重新启动时运行“dtrace”并保持运行以监控 MacOS 上的文件
- powershell - 为 CSV 文件添加标题
- javascript - 如何在同一个 DOM 控制台中访问嵌套的文档对象?
- node.js - 删除推文中的 URL 卡片 (API)