【问题标题】:How to retrieve date and time in a RecyclerView from FirebaseFirestore if i have stored time as ServerValue.TIMESTAMP in the database如果我将时间作为 ServerValue.TIMESTAMP 存储在数据库中,如何从 FirebaseFirestore 检索 RecyclerView 中的日期和时间
【发布时间】:2018-02-20 08:31:02
【问题描述】:

我正在制作一个应用程序,用户可以在其中发布信息,并在发布时通过在 Firestore 数据库中使用 ServerValue.TIMESTAMP 来节省发布时间。

问题是时间正在数据库中保存为类似这样的地图

time:
   .sv : "timestamp"

这是我在 Firestore 数据库中的图片

现在我想提取人类可读的日期和时间以在RecyclerView 中显示给用户。

这是我的模型类,我想通过它获得时间,但还没有包含任何数据成员,因为我不知道如何完成它。

public class PostDisplayModel extends TransferId{

public String teacherName;
public String topic;

PostDisplayModel(){}

public PostDisplayModel(String teacherName, String topic) {
    this.teacherName = teacherName;
    this.topic = topic;
}

public String getTeacherName() {
    return teacherName;
}

public void setTeacherName(String teacherName) {
    this.teacherName = teacherName;
}

public String getTopic() {
    return topic;
}

public void setTopic(String topic) {
    this.topic = topic;
}
}

下面是我的适配器类

public class PostDisplayAdapter extends RecyclerView.Adapter<PostDisplayAdapter.ViewHolder> {

Context context;
List<PostDisplayModel> postsList;

PostDisplayAdapter(Context context,List<PostDisplayModel> postsList){
    this.postsList = postsList;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_display_blueprint,parent,false);
    return  new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {

    holder.headline.setText(postsList.get(position).getTopic());
    holder.uploaderName.setText(postsList.get(position).getTeacherName());

    final String id = postsList.get(position).id;
    holder.mView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(context,PostDisplayActivity.class);
            intent.putExtra("id",id);
            context.startActivity(intent);

        }
    });
}

@Override
public int getItemCount() {
    return postsList.size();
}

class ViewHolder extends RecyclerView.ViewHolder{

    View mView;
    public TextView uploaderName, headline;
    public TextView time;         //this is the textview in which i want the time to get displayed

    public ViewHolder(View itemView) {
        super(itemView);
        mView = itemView;

        uploaderName = mView.findViewById(R.id.uploader_name);
        headline = mView.findViewById(R.id.headline);
        time = mView.findViewById(R.id.time);     //getting id of the time textview

    }
}
}

我想要一个正确格式的时间戳,例如 textview 中的 yyyy-MM-dd hh:mm:ss,我正在从回收站视图中正确获取所有其他数据。

我们将不胜感激任何形式的建议/帮助。

提前致谢。

【问题讨论】:

    标签: java android firebase android-recyclerview google-cloud-firestore


    【解决方案1】:

    发生这种情况是因为您将时间戳设置为ServerValue.TIMESTAMP。当您使用 Firebase 实时数据库时,这是正确的方法。对于 Cloud Firestore,您需要将其设置为 FieldValue.serverTimestamp()

    This 是您可以使用模型类在 Cloud Firestore 数据库中设置 TIMESTAMP 的正确方法。

    【讨论】:

      猜你喜欢
      • 2019-04-01
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-04
      • 2010-11-04
      • 1970-01-01
      • 2020-03-28
      相关资源
      最近更新 更多