2017-02-04 201 views
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有什麼問題嗎?

回答

2

有在你的代碼和數據結構中的一些問題:

-comments: { 
    -Kc6CkgQ8-5OztuqHqdS { 
    text: "Text here", 
    timestamp: 1486179732382, 
    user: { 
      "username": "user1", 
      "email": "[email protected]" 
    } 
    } 

如果-確實在comments前,請刪除它。該節點應該被稱爲comments,並且添加-前綴只是使其更難正確處理。

下一個是在你的代碼中的問題:在(現在)comments你的評論列表,通過推ID鍵(按鍵開始-K在你的代碼,你只建模一個評論:

public class Post { 
    private Comment comments; 

沒有辦法了火力地堡數據庫客戶端的comments所有子節點映射成一個Comment,所以當它讀取JSON它跳過它的JSON結構的正確映射:

public class Post { 
    public Map<String,Comment> comments; 

如果您只想處理評論的值,可以用post.comments.values()來獲取它們。

+0

謝謝@Frank van Puffelen。註釋中的'-'不是必需的,我只是從firebase中複製了這個。我會盡力按照你對我評論的建議。非常感謝! – whaangbuu

+0

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