2017-02-04 156 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