在我的Android应用程序中使用ServerValue.TIMESTAMP

时间:2016-07-21 16:03:45

标签: android firebase firebase-realtime-database

我已经阅读了很多与

相关的stackoverflow问题
#include "stdafx.h"
#include <iostream>
#include<vector>
#include <string>
#include <algorithm>
#define n 3

using namespace std;
class student{
public:
    int stno,score,i;
    string name;
};


int main(){
    int l;
   vector<student> my_vector;
    for(int i = 0; i < n; i++)
    {
        student new_student;

        cout << "Student Number: ";
        cin >> new_student.stno;
        cout << "Score: ";
        cin >> new_student.score;
       cout << "Name: ";
        cin >> new_student.name;

        my_vector.push_back(new_student);
    }

cout<<"which score are you thinking about?=";
cin>>l;

(find(my_vector.begin(), my_vector.end(),[] (student const& scores){
    if (scores.score ==l)
        cout<<"it is available"<<scores.name<<scores.stno;
    else
      cout<<"nit available;}));

cin.get();
cin.get();
}

但我无法弄清楚如何在我的应用中使用它。

我需要获得创建帖子的timeStamp。 应将timeStamp添加到与帖子的uid,作者等相同的位置。

撰写帖子 firebase数据库的代码段:

ServerValue.TIMESTAMP

我的 Post.java 文件

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

}

我应该如何使用

@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]

在我的代码中获取创建帖子的时间。

2 个答案:

答案 0 :(得分:4)

ServerValue.TIMESTAMP只是一个占位符值。当Firebase服务器处理更新请求并将ServerValue.TIMESTAMP作为值时,它会将其替换为当前服务器时钟。

writeNewPost()方法中,您可以添加一行来设置创建时间:

Map<String, Object> postValues = post.toMap();
postValues.put("timeStamp", ServerValue.TIMESTAMP);

如果您仅使用Post.toMap()创建帖子而不是更新,则可以在此处使用类似的语句,而不是writeNewPost()

答案 1 :(得分:0)

您可以尝试将此添加到 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
}

最后,您可以将时间戳值添加到 toMap()函数中,就像对星星HashMap一样。

最后,您可以初始化其他变量(如作者等)的值,然后可以添加此行。

Map<String, String> timestamp = ServerValue.TIMESTAMP;

并确保将 timestamp 传递给 writeNewPost()方法。