如何在Firebase数据库的主节点内获取节点?

时间:2016-12-17 23:25:15

标签: android json firebase firebase-realtime-database

我正在尝试解析firebase数据库数据。 让我展示一下数据库的结构: enter image description here

如您所见,有一个名为sessions的节点,它包含一些对象,每个会话对象中还有一个名为messages的节点,有时候没有消息节点。 我没有问题获取对象whiteout消息节点,当我想用​​消息解析对象时我有问题。

这是我的会话课程:

public class Items_Sessions implements Serializable {

public String address;
public String creator;
public String date;
public String description;
public int duration;
public double latitude;
public double longitude;
public int margin;
public String subject;
public Items_Messages messages;

public  String sessionID;
public String sessionInviteStatus="pending";

public Items_Sessions(){}



public Items_Sessions(String address,String creator,String date,String description,int duration,double lat,double longitude,int margin,
                      String subject){
    this.address = address;
    this.creator = creator;
    this.date = date;
    this.description = description;
    this.duration = duration;
    this.latitude = lat;
    this.longitude = longitude;
    this.margin = margin;
    this.subject = subject;

}

public Items_Sessions(String address,String creator,String date,String description,int duration,double lat,double longitude,int margin,
                      Items_Messages messages,String subject){
    this.address = address;
    this.creator = creator;
    this.date = date;
    this.description = description;
    this.duration = duration;
    this.latitude = lat;
    this.longitude = longitude;
    this.margin = margin;
    this.subject = subject;
    this.messages = messages;

}

}

这是我的消息类:

 public class Items_Messages {

public String senderid;
public String sendername;
public String text;


public Items_Messages(){}

public Items_Messages(String id,String name,String text){
    this.senderid = id;
    this.sendername = name;
    this.text = text;
}

}

以下是我获取数据的方式:

mRef = mDB.getReference("sessions");
mRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            List<Items_Sessions> mSessionsItems = new ArrayList<>();
            for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                try {

                        Items_Sessions session = postSnapshot.getValue(Items_Sessions.class);
                        session.sessionID = postSnapshot.getKey();
                        if (session.creator.equalsIgnoreCase(mAuth.getCurrentUser().getUid()))
                            mSessionsItems.add(session);

                }catch (Exception e){
                    Log.i("XX",e.toString());
                }


            }

正如我之前所说,没有消息的对象没有问题但是当有消息节点我得到这个错误时:

No setter/field for -KZCmBfXzYFJR2HcjMrO found on class ir.srico.locshare.items.Items_Messages

我该如何解决问题?

1 个答案:

答案 0 :(得分:1)

我认为messages中的Items_Sessions属性需要更改为:

Map<String, Items_Messages> messages;