从自定义类获取时间戳记

问题描述 投票:0回答:2

我已经搜索了两个小时,我很惊讶,没有使用自定义类从Firebase检索Timestamp的简单方法。

这是我正在检索的文档的样子:

enter image description here

我为此文档提供了简单的自定义类:

import com.google.firebase.Timestamp;

import java.util.List;

public class CapObj {
    private String content;
    private List<String> tags;
    private Timestamp created_at;

    public CapObj () {}

    public CapObj (String content, List<String> tags, Timestamp created_at){
        this.content = content;
        this.tags = tags;
        this.created_at = created_at;
    }

    public String getContent() {
        return content;
    }

    public List<String> getTags() {
        return tags;
    }

    public Timestamp getCreatedAt() {
        return created_at;
    }
}

并且我用这种方法填充列表:

private void populateDatasetFromQuery(Task<QuerySnapshot> task){

    ArrayList<Caption> results = new ArrayList<>();
    for (QueryDocumentSnapshot document : task.getResult()) {

        CapObj capObj = document.toObject(Caption.class);
        results.add(caption);
    }
    capObjList = new ArrayList<>(results);
}

所以问题是当我尝试使用capObj.getContent()时,我会得到内容,但是当我尝试capObj.getCreatedAt().toDate()时,我会得到:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Date com.google.firebase.Timestamp.toDate()' on a null object reference。即使我从Firebase查询中获得的对象看起来像这样:

{created_at=Timestamp(seconds=1583017200, nanoseconds=0), content=Travel 2, tags=[travel]}

对我来说,创建对象时似乎没有填充created_at。我在这里错了吗?

android firebase google-cloud-firestore
2个回答
0
投票

Firestore不知道如何将created_at字段映射到您的对象。您应该做的是创建一个与字段匹配的公共setter方法:

public void setCreated_at(Timestamp timestamp) {
    created_at = timestamp;
}

由于您在字段名称中使用下划线,所以设置名称很丑陋(与Java POJO的约定一样),因此您可能想给它一个更好的名称,并用PropertyName进行注释,以便Firestore知道应使用哪个字段来调用该方法:

@PropertyName("created_at")
public void setCreatedAt(Timestamp timestamp) {
    created_at = timestamp;
}

所有POJO的常规约定是具有相同属性名称(例如getFoo / setFoo)的匹配公共getter和setter。


0
投票

您可以使用timeinmillies代替timestamp,还需要使用setter方法:您可以这样:

import java.util.List;

public class CapObj {
@SerializedName("content")
private String content;
@SerializedName("tags")
private List<String> tags;
@SerializedName("created_at")
private long created_at;

public CapObj () {}

public CapObj (String content, List<String> tags){
    this.content = content;
    this.tags = tags;
    this.created_at = Calendar.getInstance().getTimeInMillis();
}


public void setContent(String content) {
    this.content = content;
}

public void setTags(List<String> tags) {
    this.tags = tags;
}

public long getCreated_at() {
    return created_at;
}

public void setCreated_at(long created_at) {
    this.created_at = created_at;
}


public String getContent() {
    return content;
}

public List<String> getTags() {
    return tags;
}


}

获取数据后,您可以使用以下方法将长时转换为日历日期时间:

Date getDateFromLong(long timeInMillis){
 Calendar time_date=Calendar.getInstance();
 time_date.setTimeInMillis(timeInMillis);
 return time_date.getTime();
  }
© www.soinside.com 2019 - 2024. All rights reserved.