无法在Firebase中添加时间戳?

时间:2017-02-03 21:37:04

标签: java android firebase

我在此完成了Firebase Android Codelab:https://codelabs.developers.google.com/codelabs/firebase-android/

发送消息时,消息的名称,photoUrl和文本已组合在一起,如下所示:

enter image description here

我正在尝试在该组中添加该邮件的时间戳(ServerValue.TIMESTAMP)。

应该发送消息的代码以及消息的名称,photoUrl和文本+时间戳(最初来自MainActivity.java):

    mSendButton = (Button) findViewById(R.id.sendButton);
    mSendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername,
                    mPhotoUrl, ServerValue.TIMESTAMP); //It's not supposed to be like this! What should I write instead?
            mFirebaseDatabaseReference.child(MESSAGES_CHILD).push().setValue(friendlyMessage);
            mMessageEditText.setText("");
            mFirebaseAnalytics.logEvent(MESSAGE_SENT_EVENT, null);
        }
    });

使用上面的代码,我无法按照自己的意愿添加时间戳。为什么?我该怎么办?

FriendlyMessage.java

    public class FriendlyMessage {

    private String id;
    private String text;
    private String name;
    private String photoUrl;
    private Long creationDate;

    public FriendlyMessage() {
    }

    public FriendlyMessage(String text, String name, String photoUrl, Long creationDate) {
        this.text = text;
        this.name = name;
        this.photoUrl = photoUrl;
        this.creationDate = creationDate;
    }

    public java.util.Map<String, String> getCreationDate() {
        return ServerValue.TIMESTAMP;
    }

    @Exclude
    public Long getCreationDateLong() {
        return creationDate;
    }

    public void setCreationDate(Long creationDate) {
        this.creationDate = creationDate;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

    public void setPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl;
    }
}

查看整个Firebase Android Codelab原始项目here(无时间戳)。

2 个答案:

答案 0 :(得分:2)

喜欢这个

public class FriendlyMessage {

    private String id;
    private String text;
    private String name;
    private String photoUrl;
    private Long creationDate;

    public FriendlyMessage() {
    }

    public FriendlyMessage(String text, String name, String photoUrl) {
        this.text = text;
        this.name = name;
        this.photoUrl = photoUrl;
    }

    public java.util.Map<String, String> getCreationDate() {
        return ServerValue.TIMESTAMP;
    }

    @Exclude
    public Long getCreationDateLong() {
        return creationDate;
    }

    public void setCreationDate(Long creationDate) {
        this.creationDate = creationDate;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

    public void setPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl;
    }
    public Map<String, Object> toMap() {
        HashMap<String, Object> result = new HashMap<>();

        result.put("id", id);
        result.put("text", text);
        result.put("name", name);
        result.put("photoUrl", photoUrl);
        return result;

    }

}

代码中的某处执行此操作

Date someDate = new Date();

FriendlyMessage friendlyMessage = new FriendlyMessage("bla","bla","bla");

 String key = mFirebaseDatabaseReference.child(MESSAGES_CHILD).push().getKey();
 Map<String, Object> postValues =  friendlyMessage.toMap();
 postValues.put("creationDate", ServerValue.TIMESTAMP);
 childUpdates.put("/"+MESSAGES_CHILD+"/" + key, postValues);
 Map<String, Object> childUpdates = new HashMap<>();

 mFirebaseDatabaseReference.updateChildren(childUpdates);

答案 1 :(得分:1)

您必须将creationDate变量的数据类型从Long更改为Map。因为ServerValue.TIMESTAMP会返回一个Map值。

以下是它的外观:

public class FriendlyMessage {

    private String id;
    private String text;
    private String name;
    private String photoUrl;
    private Map creationDate;

    public FriendlyMessage() {
    }

    public FriendlyMessage(String text, String name, String photoUrl, Map CreationDate) {
        this.text = text;
        this.name = name;
        this.photoUrl = photoUrl;
        this.creationDate = creationDate;
    }

    public java.util.Map<String, String> getCreationDate() {
        return ServerValue.TIMESTAMP;
    }

    public void setCreationDate(Map creationDate) {
        this.creationDate = creationDate;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

    public void setPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl;
    }
}