我正在与android studio合作,尝试使用firebase数据库。
我遇到了以下问题。
我想为多个恐龙创建卡片(这些在数据库中)。
所以我尝试使用FireBase提供的“onchildeded”功能。
使用数据快照,我只想添加尚未被用户“评级”的恐龙卡片。(把它看成是一个可以让人喜欢或不喜欢恐龙的小程序)。
这是我的代码:
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行有一个日志D行,它记录了当前的数据快照,如果当前用户不喜欢这个恐龙,那么就在之后。
日志行的第一部分正确地显示了数据快照,但出于某种原因,第二部分总是错误的。
下面是一次运行的日志。
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很陌生,这只是一个学校项目,所以解决方案是否真的很糟糕并不重要。如果它起作用,它就会起作用。