我在firebase中获得儿童的无效价值

发布于 2025-02-12 15:48:26 字数 721 浏览 0 评论 0原文

这是获得级别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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我喜欢麦丽素 2025-02-19 15:48:26

问题在于您试图从以下路径中读取数据的路径:

getReference().child("Users").child("levelId")

如果我们查看数据库的屏幕截图,则没有路径/users users/levelid path /users/-n63q0bzdrkahoglnryq/levelid,但这不是您从中加载的数据。

由于您正在阅读不存在的路径,因此您的onDataChange用空的快照被调用。


要加载数据,您需要了解该数据的整个路径。因此,在您的情况下:

DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users/-N63Q0bzdrKahogLNryQ/levelId");

reference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@androidx.annotation.NonNull DataSnapshot snapshot) {
       String levelid=snapshot.getValue(String.class);
       lvlHint.setText(levelid);
    }
    ...

如果您不知道-N63Q0BZDRKAHOGLNRYQ路径的一部分,那么您唯一可以做的就是加载整个用户 node node并迭代所有孩子它下面的节点,然后获取每个子节点的selvicid

DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users");

reference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@androidx.annotation.NonNull DataSnapshot snapshot) {
        for (DataSnapshot child: snapshot.getChildren()) {
            String levelid=child.getValue(String.class);
            lvlHint.setText(levelid);
        }
    }
    ...

因此,即使在屏幕截图中只有一个子节点,您仍然需要循环浏览孩子以获取该子节点的数据。

The problem is in the path you're trying to read data from:

getReference().child("Users").child("levelId")

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:

DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users/-N63Q0bzdrKahogLNryQ/levelId");

reference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@androidx.annotation.NonNull DataSnapshot snapshot) {
       String levelid=snapshot.getValue(String.class);
       lvlHint.setText(levelid);
    }
    ...

If you don't know the -N63Q0bzdrKahogLNryQ part of the path, the only thing you can do is load the entire Users node and iterate over all child nodes under it and then get the levelId of each of those child nodes.

DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users");

reference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@androidx.annotation.NonNull DataSnapshot snapshot) {
        for (DataSnapshot child: snapshot.getChildren()) {
            String levelid=child.getValue(String.class);
            lvlHint.setText(levelid);
        }
    }
    ...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文