首页 > 解决方案 > DataSnapshot 是满的还是空的?

问题描述

我正在使用 Android Studio 尝试使用 Firebase 数据库。我遇到了以下问题。我想为多个恐龙创建卡片(这些在 Db 中)。所以我尝试的是使用 Firebase 提供的“onChildAdded”功能。使用 DataSnapshot 我试图只添加尚未被用户“评级”的恐龙卡片。(把它看成一个火种应用程序,人们可以喜欢或不喜欢恐龙)。

这是我的代码:

public int counter = 0;
    private void getDinos() {
        final DatabaseReference dinosDb = FirebaseDatabase.getInstance().getReference().child("Users").child("Dinos");
        dinosDb.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                if (dataSnapshot.exists()){
                    ArrayList<String> dinoList = new ArrayList<String>(){{ add("Allosaurus"); add("Ankylosaurus"); add("Baryonyx");add("Brachiosaurus");add("Carnotaurus");add("Dilophosaurus");add("Diplodocus");add("Gallimimus");  add("Giganotosaurus"); add("Iguanodon"); add("Megalosaurus"); add("Spinosaurus"); add("Stegosaurus"); add("Triceratops"); add("Tyrannosaurus"); add("Velociraptor");}};
                    if (!dataSnapshot.child(dinoList.get(i)).child("connections").child("like").hasChild(currentUId) && !dataSnapshot.child(dinoList.get(i)).child("connections").child("dislike").hasChild(currentUId)) {
                        Log.d("mine", "" + dataSnapshot + dataSnapshot.child(dinoList.get(counter)).child("connections").child("dislike").hasChild(currentUId));//deze shit gaat dus fout
                        cards theItem = new cards(dataSnapshot.getKey(), (String) dataSnapshot.child(dinoList.get(i)).getKey());
                        rowItems.add(theItem);
                        arrayAdapter.notifyDataSetChanged();
                    }
                    counter++;
                }
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
}

如您所见,我在第 10 行有一个 Log.d 行,用于记录当前数据快照以及当前用户是否不喜欢该恐龙。

该日志行的第一部分正确显示了 dataSnapshot,但由于某种原因,第二部分总是错误的。以下是一次运行的日志。

06-19 19:48:07.602 9671-9671/com.example.baksteen_13.dinopackopening 

D/mine: DataSnapshot { key = Allosaurus, value = {dummy=Allosaurus, connections={dislike={eSUnb0kHS1eZByRpZyeRi9SrSfe2=eSUnb0kHS1eZByRpZyeRi9SrSfe2}}} }false
06-19 19:48:07.604 9671-9671/com.example.baksteen_13.dinopackopening D/mine: DataSnapshot { key = Ankylosaurus, value = {dummy=Ankylosaurus, connections={dislike={eSUnb0kHS1eZByRpZyeRi9SrSfe2=eSUnb0kHS1eZByRpZyeRi9SrSfe2}}} }false
    DataSnapshot { key = Baryonyx, value = {dummy=Baryonyx, connections={dislike={eSUnb0kHS1eZByRpZyeRi9SrSfe2=eSUnb0kHS1eZByRpZyeRi9SrSfe2}}} }false
06-19 19:48:07.605 9671-9671/com.example.baksteen_13.dinopackopening D/mine: DataSnapshot { key = Brachiosaurus, value = {dummy=Brachiosaurus, connections={like={eSUnb0kHS1eZByRpZyeRi9SrSfe2=eSUnb0kHS1eZByRpZyeRi9SrSfe2}, dislike={eSUnb0kHS1eZByRpZyeRi9SrSfe2=eSUnb0kHS1eZByRpZyeRi9SrSfe2}}} }false
06-19 19:48:07.606 9671-9671/com.example.baksteen_13.dinopackopening D/mine: DataSnapshot { key = Carnotaurus, value = {dummy=Carnotaurus, connections={dislike={eSUnb0kHS1eZByRpZyeRi9SrSfe2=eSUnb0kHS1eZByRpZyeRi9SrSfe2}}} }false
06-19 19:48:07.607 9671-9671/com.example.baksteen_13.dinopackopening D/mine: DataSnapshot { key = Dilophosaurus, value = {dummy=Dilophosaurus, connections={like={eSUnb0kHS1eZByRpZyeRi9SrSfe2=eSUnb0kHS1eZByRpZyeRi9SrSfe2}}} }false
06-19 19:48:07.609 9671-9671/com.example.baksteen_13.dinopackopening D/mine: DataSnapshot { key = Diplodocus, value = {dummy=Diplodocus, connections={dislike={eSUnb0kHS1eZByRpZyeRi9SrSfe2=eSUnb0kHS1eZByRpZyeRi9SrSfe2}}} }false
06-19 19:48:07.610 9671-9671/com.example.baksteen_13.dinopackopening D/mine: DataSnapshot { key = Gallimimus, value = {dummy=Gallimimus} }false
    DataSnapshot { key = Giganotosaurus, value = {dummy=Giganotosaurus} }false
    DataSnapshot { key = Iguanodon, value = {dummy=Iguanodon} }false
06-19 19:48:07.611 9671-9671/com.example.baksteen_13.dinopackopening D/mine: DataSnapshot { key = Megalosaurus, value = {dummy=Megalosaurus} }false
    DataSnapshot { key = Spinosaurus, value = {dummy=Spinosaurus, connections={dislike={eSUnb0kHS1eZByRpZyeRi9SrSfe2=eSUnb0kHS1eZByRpZyeRi9SrSfe2}}} }false
06-19 19:48:07.612 9671-9671/com.example.baksteen_13.dinopackopening D/mine: DataSnapshot { key = Stegosaurus, value = {dummy=Stegosaurus} }false
06-19 19:48:07.613 9671-9671/com.example.baksteen_13.dinopackopening D/mine: DataSnapshot { key = Triceratops, value = {dummy=Triceratops} }false
    DataSnapshot { key = Tyrannosaurus, value = {dummy=Tyrannosaurus, connections={like={eSUnb0kHS1eZByRpZyeRi9SrSfe2=eSUnb0kHS1eZByRpZyeRi9SrSfe2}}} }false
06-19 19:48:07.615 9671-9671/com.example.baksteen_13.dinopackopening D/mine: DataSnapshot { key = Velociraptor, value = {dummy=Velociraptor, connections={dislike={eSUnb0kHS1eZByRpZyeRi9SrSfe2=eSUnb0kHS1eZByRpZyeRi9SrSfe2}}} }false

如您所见,大多数恐龙已经被评级,但真/假输出始终为假。

最后这里是我的数据库的一部分:

    {
  "Users" : {
    "Dinos" : {
      "Allosaurus" : {
        "connections" : {
          "dislike" : {
            "eSUnb0kHS1eZByRpZyeRi9SrSfe2" : "eSUnb0kHS1eZByRpZyeRi9SrSfe2"
          }
        },
        "dummy" : "Allosaurus"
      },
      "Ankylosaurus" : {
        "connections" : {
          "dislike" : {
            "eSUnb0kHS1eZByRpZyeRi9SrSfe2" : "eSUnb0kHS1eZByRpZyeRi9SrSfe2"
          }
        },
        "dummy" : "Ankylosaurus"
      },
      "Baryonyx" : {
        "connections" : {
          "dislike" : {
            "eSUnb0kHS1eZByRpZyeRi9SrSfe2" : "eSUnb0kHS1eZByRpZyeRi9SrSfe2"
          }
        },
        "dummy" : "Baryonyx"
      },
      "Brachiosaurus" : {
        "connections" : {
          "dislike" : {
            "eSUnb0kHS1eZByRpZyeRi9SrSfe2" : "eSUnb0kHS1eZByRpZyeRi9SrSfe2"
          },
          "like" : {
            "eSUnb0kHS1eZByRpZyeRi9SrSfe2" : "eSUnb0kHS1eZByRpZyeRi9SrSfe2"
          }
        },
        "dummy" : "Brachiosaurus"
      }
    },
    "Human" : {
      "1ngP9WzFw9MZU6AiGVgo08orpvA3" : {
        "name" : "klaas"
      },
      "5O2eVDdQs6eAYCykNorMzGd66Ys2" : {
        "name" : "jetske"
      },
      "AGGUm2y3OAQzJJWEmFXkPPJVsS23" : {
        "name" : "johan"
      }
    }
  },
  "message" : "Hello, World!"
}

如果有人能提供一些关于我在这里做错了什么的见解,我将不胜感激。我对 Android Studio 真的很陌生,这只是一个学校项目,所以解决方案是否真的很糟糕并不重要。如果它有效,它就有效。

标签: javaandroidfirebasefirebase-realtime-database

解决方案


我还不能发表评论,所以我必须使用答案,但是:

看起来您并没有遍历 DataSnapshot 中的所有节点,因此您只能获得 dataSnapshot 参数的值。您没有足够深入地迭代树。

您需要一个 foreach 循环来遍历 dataSnapshot 的子节点。

试试这个并在 foreach 循环中设置一个断点并调试应用程序并查看哪些值表示为键和值。如果没有实际的应用程序可以玩,我真的无能为力,但我觉得这应该会有所帮助:

  public int counter = 0;

  private void getDinos() {
  final DatabaseReference dinosDb = 
 FirebaseDatabase.getInstance().getReference().child("Users").child("Dinos");
  dinosDb.addChildEventListener(new ChildEventListener() {
  @Override
  public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable 
 String s) {

    for(DataSnapshot ds : dataSnapshot.getChildren()) {
    if (dataSnapshot.exists()) {
      ArrayList<String> dinoList = new ArrayList<String>() {{
        add("Allosaurus");
        add("Ankylosaurus");
        add("Baryonyx");
        add("Brachiosaurus");
        add("Carnotaurus");
        add("Dilophosaurus");
        add("Diplodocus");
        add("Gallimimus");
        add("Giganotosaurus");
        add("Iguanodon");
        add("Megalosaurus");
        add("Spinosaurus");
        add("Stegosaurus");
        add("Triceratops");
        add("Tyrannosaurus");
        add("Velociraptor");
      }};
      if (!dataSnapshot.child(dinoList.get(i)).child("connections").child("like").hasChild(currentUId) && !dataSnapshot.child(dinoList.get(i)).child("connections").child("dislike").hasChild(currentUId)) {
        Log.d("mine", "" + dataSnapshot + dataSnapshot.child(dinoList.get(counter)).child("connections").child("dislike").hasChild(currentUId));//deze shit gaat dus fout
        cards theItem = new cards(dataSnapshot.getKey(), (String) dataSnapshot.child(dinoList.get(i)).getKey());
        rowItems.add(theItem);
        arrayAdapter.notifyDataSetChanged();
      }
    }
      counter++;
    }
  }

  @Override
  public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

  }

  @Override
  public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

  }

  @Override
  public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

  }

  @Override
  public void onCancelled(@NonNull DatabaseError databaseError) {

  }
});

}


推荐阅读