簡體   English   中英

無法在Firebase中添加時間戳?

[英]Cannot add timestamp in Firebase?

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

發送消息時,消息的名稱,photoUrl和文本已分組在一起,如下所示:

在此處輸入圖片說明

我正在嘗試在該組中添加該消息的時間戳(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原始項目(無時間戳)。

像這樣

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);

您必須將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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM