我在firebase中获得儿童的无效价值
这是获得级别ID值的代码:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users").child("levelId");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@androidx.annotation.NonNull DataSnapshot snapshot) {
String levelid=snapshot.getValue(String.class);
lvlHint.setText(levelid);
}
@Override
public void onCancelled(@androidx.annotation.NonNull DatabaseError error) {
Toast.makeText(MainActivity.this,error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
这是我的数据库快照,我想从此表中获取“ levelId”和“ userId”。
Here's the code of getting value of level id:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users").child("levelId");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@androidx.annotation.NonNull DataSnapshot snapshot) {
String levelid=snapshot.getValue(String.class);
lvlHint.setText(levelid);
}
@Override
public void onCancelled(@androidx.annotation.NonNull DatabaseError error) {
Toast.makeText(MainActivity.this,error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
This is my database snapshot and I want to get "levelId" and "userId" from this table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于您试图从以下路径中读取数据的路径:
如果我们查看数据库的屏幕截图,则没有路径
/users users/levelid
。 是 path/users/-n63q0bzdrkahoglnryq/levelid
,但这不是您从中加载的数据。由于您正在阅读不存在的路径,因此您的
onDataChange
用空的快照被调用。要加载数据,您需要了解该数据的整个路径。因此,在您的情况下:
如果您不知道
-N63Q0BZDRKAHOGLNRYQ
路径的一部分,那么您唯一可以做的就是加载整个用户
node node并迭代所有孩子它下面的节点,然后获取每个子节点的selvicid
。因此,即使在屏幕截图中只有一个子节点,您仍然需要循环浏览孩子以获取该子节点的数据。
The problem is in the path you're trying to read data from:
If we look at the screenshot of your database, there is no path
/Users/levelId
. There is a path/Users/-N63Q0bzdrKahogLNryQ/levelId
, but that is not what you're loading data from.Since you're reading a path that doesn't exist, your
onDataChange
gets called with an empty snapshot.To load the data you will need to know the entire path to that data. So in your case:
If you don't know the
-N63Q0bzdrKahogLNryQ
part of the path, the only thing you can do is load the entireUsers
node and iterate over all child nodes under it and then get thelevelId
of each of those child nodes.So even when there's only one child node as in your screenshot, you will still need to loop over the children to get at the data of that child node.