Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在firestore或实时数据库中使用TimeAgo格式?_Android_Kotlin_Google Cloud Firestore_Android Dateutils - Fatal编程技术网

Android 如何在firestore或实时数据库中使用TimeAgo格式?

Android 如何在firestore或实时数据库中使用TimeAgo格式?,android,kotlin,google-cloud-firestore,android-dateutils,Android,Kotlin,Google Cloud Firestore,Android Dateutils,我正在使用一个文本视图来显示时间,格式就像刚才,昨天,时间之前。。。等等。它显示我想要的时间。但问题是,时间并没有像刚刚发布时那样改变,而是永远不变 postAdapter的模型类 从Firestore检索数据 您要做的是使用库将日期转换为字符串,然后将TextView文本设置为该字符串。字符串只是字符列表,它们不知道它们的含义,也不知道它们是否需要更改/更新 我建议使用android上的RelativeTimeTextView。一旦您设置了该视图的参考时间,其中的一些代码将自动为您触发更新 编

我正在使用一个文本视图来显示时间,格式就像刚才,昨天,时间之前。。。等等。它显示我想要的时间。但问题是,时间并没有像刚刚发布时那样改变,而是永远不变

postAdapter的模型类

从Firestore检索数据


您要做的是使用库将日期转换为字符串,然后将TextView文本设置为该字符串。字符串只是字符列表,它们不知道它们的含义,也不知道它们是否需要更改/更新

我建议使用android上的RelativeTimeTextView。一旦您设置了该视图的参考时间,其中的一些代码将自动为您触发更新


编辑:在firestore中存储时,您还正在进行从timeInMillis:Long到date:String的转换。相反,您应该将timeInMillis存储为长值,然后在读取时将其用作relativeTimetextView.SetReferenceTimeinmillis的参数。

您要做的是使用库将日期转换为字符串,然后将TextView文本设置为该字符串。字符串只是字符列表,它们不知道它们的含义,也不知道它们是否需要更改/更新

我建议使用android上的RelativeTimeTextView。一旦您设置了该视图的参考时间,其中的一些代码将自动为您触发更新



编辑:在firestore中存储时,您还正在进行从timeInMillis:Long到date:String的转换。您应该将timeInMillis存储为Long值,然后在读取时将其用作RelativeTimeExtView.SetReferenceTimeinmillis的参数。

我使用了您建议的参数,但我正在做相同的事情,因此请告诉我应该如何处理该相对布局。我是说那之后我该怎么办,该视图将如何显示时间和随时间变化。您应该考虑在没有使用任何转换的情况下存储TimeMILLI. BROO,当我使用long时,它显示了一个较长的值,正如刚才159038、463687.5。您可以共享使用相对TimeExtVIEW的代码吗?我没有使用LIBI使用过您建议的,但我也在做同样的事情。请告诉我,我该如何处理相对布局。我是说那之后我该怎么办,该视图将如何显示时间和随时间变化。您应该考虑在没有使用任何转换的情况下存储TimeMILLI. BROO,当我使用long时,它显示了一个较长的值,就像刚才159038、463687.5。您可以共享使用RealTimeTeTeVIEW的代码吗?我不使用LIbStudio如何查看FiSt店中存储日期的截图。请更新问题检查。post?.getDate返回的是什么?长值是字符串,对吗?是@AlexMamo:看来您仍在接收长值。您是否尝试过使用“.ToString”进一步转换“timeAgo”?除此之外,此日期值是否对应Firestore时间戳?你有没有尝试过使用其他库来代替这个特定的时间段?展示一个屏幕截图来查看日期是如何存储在Firestore中的。我更新了问题检查。post是什么?.getDate返回的是一个字符串形式的长值,对吗?是@AlexMamo:看起来你仍然收到一个长值。您是否尝试过使用“.ToString”进一步转换“timeAgo”?除此之外,此日期值是否对应Firestore时间戳?你有没有尝试过利用其他图书馆来代替这个特定的时间?
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{...}
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()
                    } 
            }
    }