Firebase 检索 child Android
Firebase retrieve child Android
我正在 Android 上访问 Firebase。我想从树中检索 child。我的代码是(已更新)
f.child("Germany").child("Username").child("Alan Turing").addChildEventListener(new ChildEventListener() {
// Retrieve new posts as they are added to Firebase
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
Map<String, Object> newPost = (Map<String, Object>) snapshot.getValue();
System.out.println("Author: " + newPost.get("Message"));
disp_msg = (TextView)findViewById(R.id.display_msg);
disp_msg.setText(newPost.get("Message").toString());
// System.out.println("Title: " + newPost.get("title"));
}
在我的 firebase 中我有
Language
Germany
Username
Alan Turing
Message
-Jf6ShYy7niHrqg_x4Tc: "tom"
-Jf9v0xHAxINUUANrORU: "perfect"
当我运行上面的代码时,我得到:
Author: {
Username={
Alan Turing={
Message={
-Jf6ShYy7niHrqg_x4Tc=tom,
-Jf9v0xHAxINUUANrORU=perfect!
}
}
}
}
但我只想要消息,而不是整个 Firebase 数据库。
那么我如何才能得到指定的 child?
目前只有这个有效,但 returns 整个 Firebase:
f.addChildEventListener(new ChildEventListener() {
// Retrieve new posts as they are added to Firebase
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
Map<String, Object> newPost = (Map<String, Object>) snapshot.getValue();
System.out.println("Author: " + newPost.get("Germany"));
disp_msg = (TextView)findViewById(R.id.display_msg);
disp_msg.setText(newPost.get("Germany").toString());
// System.out.println("Title: " + newPost.get("title"));
}
我存储如下:
Firebase usersRef = f.child("Language");
usersRef .child("Germany").child("Username").child("Alan Turing").child("Message").push().setValue(writeMsgField.getText().toString());
您可以通过从根构建 Firebase 引用来直接寻址子节点:
f.child("Germany")
.child("Username")
.child("Alan Turing")
.child("Message")
.addChildEventListener...
或者更简洁一点:
f.child("Germany/Username/Alan Turing/Message").addChildEventListener....
你真的应该遵循 Firebase Android guide, because the data structure is covered quite well in the topic on how data is structured in Firebase。现在花一些时间阅读该指南将为您以后节省很多时间(以及很多问题)。
我正在 Android 上访问 Firebase。我想从树中检索 child。我的代码是(已更新)
f.child("Germany").child("Username").child("Alan Turing").addChildEventListener(new ChildEventListener() {
// Retrieve new posts as they are added to Firebase
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
Map<String, Object> newPost = (Map<String, Object>) snapshot.getValue();
System.out.println("Author: " + newPost.get("Message"));
disp_msg = (TextView)findViewById(R.id.display_msg);
disp_msg.setText(newPost.get("Message").toString());
// System.out.println("Title: " + newPost.get("title"));
}
在我的 firebase 中我有
Language
Germany
Username
Alan Turing
Message
-Jf6ShYy7niHrqg_x4Tc: "tom"
-Jf9v0xHAxINUUANrORU: "perfect"
当我运行上面的代码时,我得到:
Author: {
Username={
Alan Turing={
Message={
-Jf6ShYy7niHrqg_x4Tc=tom,
-Jf9v0xHAxINUUANrORU=perfect!
}
}
}
}
但我只想要消息,而不是整个 Firebase 数据库。
那么我如何才能得到指定的 child?
目前只有这个有效,但 returns 整个 Firebase:
f.addChildEventListener(new ChildEventListener() {
// Retrieve new posts as they are added to Firebase
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
Map<String, Object> newPost = (Map<String, Object>) snapshot.getValue();
System.out.println("Author: " + newPost.get("Germany"));
disp_msg = (TextView)findViewById(R.id.display_msg);
disp_msg.setText(newPost.get("Germany").toString());
// System.out.println("Title: " + newPost.get("title"));
}
我存储如下:
Firebase usersRef = f.child("Language");
usersRef .child("Germany").child("Username").child("Alan Turing").child("Message").push().setValue(writeMsgField.getText().toString());
您可以通过从根构建 Firebase 引用来直接寻址子节点:
f.child("Germany")
.child("Username")
.child("Alan Turing")
.child("Message")
.addChildEventListener...
或者更简洁一点:
f.child("Germany/Username/Alan Turing/Message").addChildEventListener....
你真的应该遵循 Firebase Android guide, because the data structure is covered quite well in the topic on how data is structured in Firebase。现在花一些时间阅读该指南将为您以后节省很多时间(以及很多问题)。