Reputation: 395
I have a firebase object that looks like this
posts: {
-Kc6CT_CF--kVYApIhD9: {
-comments: {
-Kc6CkgQ8-5OztuqHqdS {
text: "Text here",
timestamp: 1486179732382,
user: {
"username": "user1",
"email": "[email protected]"
}
}
},
"lat": "37.8136",
"lng": "144.9631"
}
}
And I have a POJO that looks like this:
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:
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
. Now, my problem is I cant get the data in comments
object. When I run the debugger my comments
object is null
. What am I missing? Or is there something wrong with my POJO?
Upvotes: 3
Views: 1424
Reputation: 600126
There are a few problems in your code and data structure:
-comments: {
-Kc6CkgQ8-5OztuqHqdS {
text: "Text here",
timestamp: 1486179732382,
user: {
"username": "user1",
"email": "[email protected]"
}
}
If that -
is indeed in front of comments
, please remove it. The node should just be called comments
and adding a -
prefix just makes it more difficult to handle correctly.
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:
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. The correct mapping of your JSON structure is:
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()
.
Upvotes: 3