gpt4 book ai didi

java - DataSnapshot 同时是满的和空的?

转载 作者:行者123 更新时间:2023-11-29 23:50:38 28 4
gpt4 key购买 nike

我正在使用 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

如您所见,大多数恐龙都已被评级,但 true/false 输出始终为 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 的新手,这只是一个学校项目,所以解决方案是否非常糟糕并不重要。如果它有效,它就有效。

最佳答案

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

看起来您并没有遍历 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) {

}
});

关于java - DataSnapshot 同时是满的和空的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50936164/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com