![](/img/trans.png)
[英]Firebase Android ServerValue.Timestamp Map to Long
[英]Using ServerValue.TIMESTAMP in my Android App
我已经阅读了很多与
ServerValue.TIMESTAMP
但我不知道如何在我的应用程序中使用它。
我需要获取创建帖子的时间戳。 时间戳应添加到与帖子的 uid、作者等相同的位置。
将帖子写入firebase 数据库的代码片段:
private void writeNewPost(String userId, String username, String title, String body) {
// Create new post at /user-posts/$userid/$postid and at
// /posts/$postid simultaneously
String key = mDatabase.child("posts").push().getKey();
Post post = new Post(userId, username, title, body);
Map<String, Object> postValues = post.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/posts/" + key, postValues);
childUpdates.put("/user-posts/" + userId + "/" + key, postValues);
mDatabase.updateChildren(childUpdates);
}
我的Post.java文件
@IgnoreExtraProperties
public class Post {
public String uid;
public String author;
public String title;
public String body;
public int starCount = 0;
public Map<String, Boolean> stars = new HashMap<>();
public Long timeStamp;
public Post() {
// Default constructor required for calls to DataSnapshot.getValue(Post.class)
}
public Post(String uid, String author, String title, String body) {
this.uid = uid;
this.author = author;
this.title = title;
this.body = body;
}
// [START post_to_map]
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("uid", uid);
result.put("author", author);
result.put("title", title);
result.put("body", body);
result.put("starCount", starCount);
result.put("stars", stars);
return result;
}
// [END post_to_map]
}
我应该如何使用
ServerValue.TIMESTAMP
在我的代码中获取创建帖子的时间。
ServerValue.TIMESTAMP只是一个占位符值。 当 Firebase 服务器处理更新请求并找到ServerValue.TIMESTAMP
作为值时,它会将其替换为当前服务器时钟。
在您的writeNewPost()
方法中,您可以添加一行来设置创建时间:
Map<String, Object> postValues = post.toMap();
postValues.put("timeStamp", ServerValue.TIMESTAMP);
如果您只使用Post.toMap()
来创建帖子,而不是用于更新,您可以在那里放置一个类似的语句,而不是在writeNewPost()
。
您可以尝试将此添加到您的Post.java
//Declaring the timestamp variable
public Map timestamp;
//add it to your constructor
public Post(String uid, String author, String title, String body, Map timestamp) {
this.uid = uid;
this.author = author;
this.title = title;
this.body = body;
this.timestamp = timestamp
}
最后,您可以像添加星星 HashMap 一样将时间戳值添加到您的toMap()函数中。
最后,您是否为其他变量(如 Author 等)初始化了您的值,然后您可以添加这一行。
Map<String, String> timestamp = ServerValue.TIMESTAMP;
还要确保将时间戳传递给writeNewPost()方法。
您应该创建Post.java model class 文件以包含timestamp
字段:
public String uid, title, body;
public long timestamp;
public Post();
public Post(String u, String t, String b){
this.uid = u;
this.title = t;
this.body = b;
}
@Exclude
public Map<String, Object> toMap(){
Map<String, Object> map = new HashMap<>();
map.put("timestamp", ServerValue.TIMESTAMP);
map.put("uid", uid);
map.put("title", title);
map.put("body", body);
return map;
}
需要默认构造函数来读取您的 Post model 表单getValue()
。
要从您的writeNewPost()
function 中使用的路径读取您的帖子数据:
public void readPost(){
mDatabase.getReference("/posts").addChildEventListener(new ChildEventListener(){
public void onChildAdded(DataSnapshot, String previousChildName){
Post post = snapshot.getValue(Post.class);
Log.i(TAG, "time:" + post.timestamp);
}
// .....
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.