DataSnapshot同时充满和空吗?

时间:2018-06-19 20:06:38

标签: java android firebase firebase-realtime-database

我正在使用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,并在当前用户不喜欢该恐龙后立即记录。

该日志行的第一部分正确显示了dataSnapshot,但是由于某些原因,第二部分始终为false。 以下是一次运行的日志。

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的新手,这只是一个学校项目,因此该解决方案是否真的很糟糕并不重要。如果有效,就可以。

1 个答案:

答案 0 :(得分:1)

我无法发表评论,所以我必须使用答案,但:

似乎您没有遍历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) {

  }
});

}