【发布时间】:2017-06-21 08:44:57
【问题描述】:
我在这里完成了 Firebase Android Codelab:https://codelabs.developers.google.com/codelabs/firebase-android/
发送消息时,消息的名称、照片网址和文本已组合在一起,如下所示:
我正在尝试在组中添加该消息的时间戳 (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);
}
});
使用上面的代码,我无法随意添加时间戳。为什么?我该怎么办?
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(无时间戳)。
【问题讨论】: