1

我正在使用Github 库以格式显示时间,TextView例如现在、昨天、前一段时间......等等。它显示我想要的时间。但问题是,时间并没有像现在在发布时那样改变——它永远保持不变。

postAdapter的模型类

class Post {
private var date: String = ""
constructor(date: Long) {
fun getDate(): String
    {
        return date
    }

   //Initialization according to the library
    val timeInMillis = System.currentTimeMillis()
    val timeAgo = TimeAgo.using(timeInMillis)


   //saving the data to firestore
    val fStore: FirebaseFirestore = FirebaseFirestore.getInstance()
                    val post = Post(title, description, date = timeAgo)
                    fStore.collection("Posts").document()
                            .set(post)
                            .addOnSuccessListener{...}

Firestore

private fun postInfo(title: TextView, description: TextView, date: TextView) {
        val postRef = FirebaseFirestore.getInstance().collection("Posts").document()
        postRef.get()
                .addOnSuccessListener { documentSnapshot ->
                    if (documentSnapshot != null && documentSnapshot.exists()) {
                        val post = documentSnapshot.toObject(Post::class.java)
                        date.text = post?.getDate()
                        title.text = post?.getTitle()
                        description.text = post?.getDescription()
                    } 
            }
    }

在此处输入图像描述

4

1 回答 1

3

您所做的是使用库将日期转换为字符串,然后将 TextView 文本设置为该字符串。字符串只是字符列表,它们对它们的含义或是否需要更改/更新没有“意识”。

我建议使用RelativeTimeTextViewfrom android-ago。一旦你为那个视图设置了一个参考时间,里面有一些代码会自动为你触发更新。

https://github.com/curioustechizen/android-ago

编辑:您还在Firestore 中存储时timeInMillis: Long进行转换。date: String您应该按原样存储 timeInMillis (长值),然后在阅读时将其用作relativeTimetextView.setReferenceTime(timeInMillis).

于 2020-05-24T16:04:57.787 回答