Relm Relm - 1 year ago 60
Android Question

Firebase : How to put the currently returned node's key into a POJO as a field

I hope to improve the title of this question

Assume I have the following.

abstract class getMessage {
public getMessage(String k) {

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

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

The Message Class has a field "messageid", which should be the key of the returned node, but the returned data is the child of this key.

Before this, I inserted the fields with :

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

As you can see above, I was able to take the "messageid" and put it into the Message object, but using the new approach(First Code block), I don't know how to do this.

Is it possible?


THE Message Class :

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;

Answer Source

You can do this by adding a setter to the Message class and then call that method to set the key.

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

inside the onDataChange method

Message msg = d.getValue(Message.class);