Ich habeMit ServerValue.TIMESTAMP in meinem Android App
Zusammenhang viele Fragen Stackoverflow lesenServerValue.TIMESTAMP
aber ich kann nicht herausfinden, wie es in meiner App zu verwenden.
Ich brauche den timeStamp der Erstellung eines Beitrags. Der timeStamp sollte an derselben Stelle wie bei uid, author etc. des Posts hinzugefügt werden.
-Code-Schnipsel, die den Pfosten die Feuerbasis Datenbank schreibt:
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);
}
Meine Post.java Datei
@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]
}
Wie soll ich
ServerValue.TIMESTAMP
in meinem Code, um die Zeit der Erstellung von Post zu bekommen.
Ich möchte, dass die Variable timeStamp (in Post.java) sollte den Wert der Post-Erstellung enthalten. Wie mache ich das –