Firebase:如何将当前返回的节点的密钥作为字段放入POJO中

时间:2016-09-29 07:15:02

标签: android firebase hashmap firebase-realtime-database

我希望改善这个问题的标题

假设我有以下内容。

abstract class getMessage {
      public getMessage(String k) {
         get(k);
      }

      public abstract void Success(Message message);
      private void get(final String k) {
         DatabaseReference r    = DB.child("chat/messages/" + k);
         r.addListenerForSingleValueEvent(new ValueEventListener(){
            @Override
            public void onDataChange(DataSnapshot d) {
               Message msg = d.getValue(Message.class);
               Success(msg);
            }

            @Override
            public void onCancelled(DatabaseError e) {
               Log.w("Get Message", e.toException());
            }
         });
      }
   }

消息类有一个字段“messageid”,它应该是返回节点的键,但返回的数据是此键的子节点。

在此之前,我插入了以下字段:

Long time         = d.child("time").getValue();
Long String       = d.child("text").getValue();
Long messageid    = d.getKey();
Message message   = new Message(time, messageid, text);

如上所示,我可以使用“messageid”并将其放入消息对象,但使用新方法(第一个代码块) ,我不知道该怎么做。

有可能吗?

编辑

消息类:

public class Message implements Serializable {
   private String  name;
   private String  msg;
   private String  icon;
   private String  recipient;
   private String  sender;
   private String  shared;
   private Boolean group;
   private Object  time;
   private String  messageid;
   private Boolean local;
   private Boolean delivered;
   private Boolean read;
   private HashMap<String, Boolean> allowed;

   private String file;
   private Boolean attachment;
   private Boolean state;


   public Message(String Name, String Msg, String Recipient, String Sender, String Shared, Boolean Group, String Icon,
                  Object Time, String Messageid, Boolean Local, Boolean Delivered, Boolean Read,
                  HashMap<String, Boolean> Allowed, String File, Boolean Attachment, Boolean State) {
      name = Name;
      msg = Msg;
      recipient = Recipient;
      sender = Sender;
      shared = Shared;
      group = Group;
      time = Time;
      icon = Icon;
      messageid = Messageid;
      local = Local;
      delivered = Delivered;
      read = Read;
      file  = File;
      allowed   = Allowed;
      attachment    = Attachment;
      state         = State;
   }

   public Message() {

   }

   public Boolean getAttachment() {
      return attachment;
   }

   public Boolean getState() {
      return state;
   }

   public HashMap<String, Boolean> getAllowed() {
      return allowed;
   }

   public String getFile() {
      return file;
   }

   public Boolean getDelivered() {
      return delivered;
   }

   public Boolean getRead() {
      return read;
   }

   public Boolean getLocal() {
      return local;
   }

   public String getMessageid() {
      return messageid;
   }

   public String getName() {
      return name;
   }

   public Object getTime() {
      return time;
   }

   public String getMsg() {
      return msg;
   }

   public Boolean getGroup() {
      return group;
   }

   public String getRecipient() {
      return recipient;
   }

   public String getSender() {
      return sender;
   }

   public String getShared() {
      return shared;
   }

   public String getIcon() {
      return icon;
   }
}

1 个答案:

答案 0 :(得分:4)

您可以通过向Message类添加setter来执行此操作,然后调用该方法来设置密钥。

public void setMessageid(String messageId) {
    this.messageid = messageId;
}

onDataChange方法

Message msg = d.getValue(Message.class);
msg.setMessageId(d.getKey());
Success(msg);