I have list of events in recyclerview and in each ...
# android
c
I have list of events in recyclerview and in each row I want to show the time elapsed since the event with precision to seconds. The event objects have property time as Instant (from java8 datetime api). How can I update the time data every second?
stackoverflow 3
m
cuddlecheek: I can share a working solution with you, but it is designed to work with Date & Calendar.
👍 1
c
Oh, great, that would be awesome.
c
Thank you, but I don't have a problem with counting the time, I'm struggling with the UI updating
m
@cuddlecheek oh, quite misunderstood. This is a ViewHolder example, should call
bindTime
each `onBindViewHolder`:
Copy code
abstract class SomeStuffHolder(
        private val handler: Handler,
        private val date: ReactiveDateCarrier,
        view: View,
        private val timeView: TextView
) : CoolBaseHolder(view), Runnable {

    private lateinit var time: Date
    fun bindTime(time: Date) {
        handler.removeCallbacks(this)
        this.time = time
        run()
    }
    override fun run() {
        time.relativeToNowWithTimeout(date)
        timeView.text = date.text
        handler.postDelayed(this, date.timeout)
    }
    final override fun onRecycle() {
        handler.removeCallbacks(this)
    }
}
c
Thanks! your code helped me realize my silly mistake