在我的Android应用中,我使用Google Firebase将信息存储在数据库中。
我需要遍历信息,并且正在使用以下for循环
public void showDataLobReq(DataSnapshot dataSnapshot){
for(DataSnapshot ds : dataSnapshot.child("Lobby_Requests").getChildren()){
System.out.println("asdfasdfasdfasdfasdf"+ds.getValue());
game = ds.child(userID).child("game").getValue(String.class);
console = ds.child(userID).child("console").getValue(String.class);
mic = ds.child(userID).child("mic").getValue(String.class);
players = ds.child(userID).child("players").getValue(String.class);
}
}
你可能会注意到我把' .child(" Lobby_Requests")'在dataSnapshot之后。这是因为dataSnapshot获取整个数据库的快照,所以我必须进入子目录" Lobby_Requests"因为那是我需要迭代的信息。
把这个' .child()' in存在问题。
我在for循环的第一行和.child(" Lobby_Requests")中打印到dataSnapshot所包含的内容,它从目录中提取信息" Lobbies"在实际的数据库中,这是一个完全不同的目录。
然而,当我删除' .child()'完全它给了我一个像它应该的整个数据库的视图。为什么要这样做?
听众代码:
nRef = mFirebaseDatabase.getReference();
nRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
showDataLobReq(dataSnapshot);
} else {
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
这是JSON:
{
"Games" : {
"Forza 6" : {
"Consoles" : {
"PC" : true,
"Xbox 1" : true,
"Xbox 360" : true
},
"FilePathName" : "forza6",
"Genres" : {
"Racing" : true
},
"Live Lobbies" : 0,
"Name" : "Forza 6"
},
"Minecraft" : {
"Consoles" : {
"PC" : true,
"Xbox 1" : true,
"Xbox 360" : true
},
"FilePathName" : "minecraft",
"Genres" : {
"Adventure" : true,
"Creation" : true,
"Open World" : true
},
"Live Lobbies" : 0,
"Name" : "Minecraft"
}
},
"Lobbies" : {
"Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : {
"Messages" : {
"-Kq6-1HsMvElEXZZyCIk" : {
"messageText" : "hey",
"messageTime" : 1501208519771,
"messageUser" : ""
}
},
"console" : "Origin",
"game" : "Minecraft",
"leader" : "Cd6lVd2XMUYoLH6b0xoHsrfXMud2",
"mic" : "Mic",
"note" : "2345",
"players" : "4"
},
"KUWH5f1TmYfO1O1wgCJLli3XZFi2" : {
"console" : "Steam",
"game" : "Forza 6",
"mic" : "No Mic",
"note" : "Hey Join Here!",
"players" : "2"
},
"hpWkq0D8clPReUetOq9Xtmc4V582" : {
"Messages" : {
"-Kq5a0kX305lFCRTSM_G" : {
"messageText" : "hello",
"messageTime" : 1501201701014,
"messageUser" : ""
},
"-Kq5asufOWQwtmyNJrQ7" : {
"messageText" : "hey",
"messageTime" : 1501201926941,
"messageUser" : ""
}
},
"console" : "Xbox One",
"game" : "Minecraft",
"leader" : "hpWkq0D8clPReUetOq9Xtmc4V582",
"mic" : "Mic",
"note" : "kjhg",
"players" : "4"
}
},
"Lobby_Requests" : {
"Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : {
"Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : {
"console" : "Xbox One",
"game" : "Forza 6",
"mic" : "Mic",
"players" : "5"
}
},
"KUWH5f1TmYfO1O1wgCJLli3XZFi2" : {
"KUWH5f1TmYfO1O1wgCJLli3XZFi2" : {
"console" : "Steam",
"game" : "Forza 6",
"mic" : "No Mic",
"players" : "2"
}
},
"hpWkq0D8clPReUetOq9Xtmc4V582" : {
"hpWkq0D8clPReUetOq9Xtmc4V582" : {
"console" : "Xbox One",
"game" : "Minecraft",
"mic" : "Mic",
"players" : "4"
},
"players" : "4"
}
},
"users" : {
"8cHrNCybwjO3PIUKxyOLiAqxJBv1" : {
"gamertag" : "thedylan",
"uname" : "thedood"
},
"Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : {
"gamertag" : "dmdylan",
"uname" : "ninja goat"
},
"KUWH5f1TmYfO1O1wgCJLli3XZFi2" : {
"gamertag" : "skaner",
"uname" : "asdf"
},
"YvYEIiCBUSYKTviVyWpLHdyDIFw1" : {
"gamertag" : "joejoe",
"uname" : "Jifflingly"
},
"ZmX9yIZ6MNguQa1S3MaYNcxfK2b2" : {
"gamertag" : "dmkaner",
"uname" : "dmkaner"
},
"hpWkq0D8clPReUetOq9Xtmc4V582" : {
"gamertag" : "dmkaner",
"uname" : "dmkaner"
},
"t21ncnuRmeV4F7RknETBisMrxS42" : {
"gamertag" : "asdf",
"uname" : "asdf"
}
}
}
答案 0 :(得分:1)
您的代码中存在的问题是两次推送数据,并且不需要这样做。
"Lobby_Requests" : {
"Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : {
"Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : { //This is wrong
如果您通过将数据仅推送一次来更改向Firebase数据库添加数据的方式,那么您的代码将正常运行。您的数据库应如下所示:
"Lobby_Requests" : {
"Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : {
"console" : "Xbox One",
"game" : "Forza 6",
"mic" : "Mic",
"players" : "5"
},
正如您可能看到的,只有一个按键。