[英]Firebase Query - Accessing nested object in Firebase Android
I have a firebase object that looks like this 我有一个看起来像这样的firebase对象
posts: {
-Kc6CT_CF--kVYApIhD9: {
-comments: {
-Kc6CkgQ8-5OztuqHqdS {
text: "Text here",
timestamp: 1486179732382,
user: {
"username": "user1",
"email": "some@email.com"
}
}
},
"lat": "37.8136",
"lng": "144.9631"
}
}
And I have a POJO that looks like this: 我有一个看起来像这样的POJO:
public class Post {
private Comment comments;
private double lat;
private double lng;
private String text;
private String timestamp;
private Motorist user;
public Post() { }
public Comment getComments() {
return comments;
}
public void setComments(Comment comments) {
this.comments = comments;
}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLng() {
return lng;
}
public void setLng(double lng) {
this.lng = lng;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public Motorist getUser() {
return user;
}
public void setUser(Motorist user) {
this.user = user;
}
public static class Comment {
String text;
String timestamp;
Motorist user;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public Motorist getUser() {
return user;
}
public void setUser(Motorist user) {
this.user = user;
}
}
}
Here's the code where I loop in my posts
object: 这是我在posts
对象中循环的代码:
Map<String, Post> td = new HashMap<String, Post>();
for(DataSnapshot postSnapshot : dataSnapshot.getChildren()){
Post post = postSnapshot.getValue(Post.class);
//for (DataSnapshot commentSnapshot : postSnapshot.child("comments").getChildren()) {
// System.out.println(commentSnapshot.getValue());
//}
td.put(postSnapshot.getKey(), post);
Toast.makeText(MapsActivity.this, postSnapshot.getValue().toString(), Toast.LENGTH_SHORT).show();
}
I am using the addValueEventListener
. 我正在使用addValueEventListener
。 Now, my problem is I cant get the data in comments
object. 现在,我的问题是我无法获取comments
对象中的数据。 When I run the debugger my comments
object is null
. 当我运行调试器时,我的comments
对象为null
。 What am I missing? 我错过了什么? Or is there something wrong with my POJO? 或者我的POJO有问题吗?
There are a few problems in your code and data structure: 您的代码和数据结构中存在一些问题:
-comments: {
-Kc6CkgQ8-5OztuqHqdS {
text: "Text here",
timestamp: 1486179732382,
user: {
"username": "user1",
"email": "some@email.com"
}
}
If that -
is indeed in front of comments
, please remove it. 如果-
确实在comments
面前,请将其删除。 The node should just be called comments
and adding a -
prefix just makes it more difficult to handle correctly. 该节点应该只被称为comments
并且添加-
前缀只会使其更难以正确处理。
Next up is a problem in your code: under (now) comments
you have a list of comments, keyed by a push ID (the keys starting with -K
. In your code you've only modeled a single comment: 接下来是你的代码中的一个问题:在(现在) comments
你有一个注释列表,用推送ID键入(以-K
开头的键。在你的代码中,你只建模了一个注释:
public class Post {
private Comment comments;
There is no way that the Firebase Database client can map all child nodes of comments
into that single Comment
, so it skips it when it reads the JSON. Firebase数据库客户端无法将comments
所有子节点映射到该单个Comment
,因此在读取JSON时会跳过它。 The correct mapping of your JSON structure is: JSON结构的正确映射是:
public class Post {
public Map<String,Comment> comments;
If you then only want to handle the values of the comments, you can get them with post.comments.values()
. 如果您只想处理注释的值,可以使用post.comments.values()
获取它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.