0
我有一個火力點的對象,看起來像這樣火力地堡查詢 - 在火力地堡訪問嵌套的對象的Android
posts: {
-Kc6CT_CF--kVYApIhD9: {
-comments: {
-Kc6CkgQ8-5OztuqHqdS {
text: "Text here",
timestamp: 1486179732382,
user: {
"username": "user1",
"email": "[email protected]"
}
}
},
"lat": "37.8136",
"lng": "144.9631"
}
}
而且我有一個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;
}
}
}
下面的代碼,我環在我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();
}
我使用addValueEventListener
。現在,我的問題是我無法獲取comments
對象中的數據。當我運行調試器時,我的comments
對象是null
。我錯過了什麼?或者我的POJO有什麼問題嗎?
謝謝@Frank van Puffelen。註釋中的'-'不是必需的,我只是從firebase中複製了這個。我會盡力按照你對我評論的建議。非常感謝! – whaangbuu
Hi @Frank van Puffelen。我有個問題。爲什麼當我查詢來自Firebase的'comments'數據時,它只返回最後一條記錄。當我嘗試記錄器時,它顯示所有的「評論」記錄。 這裏是我的[POJO(http://www.writeurl.com/text/e06xkqe21h9kp6vxut34/3yr4yi8filndtrnpkuys/eqc01dyr1lwgap10sef5): 這裏是我的[循環](http://www.writeurl.com/text/fjo7xbuwlx5gcnplmnft/o2p7c1mevuh8ddfc5ewx/xt5vbru3e7e8mlgxz5rh)獲取'posts'以及'comments' ... 我的結構有什麼問題?截至目前,我仍然在學習這項技術。 – whaangbuu