https://kotlinlang.org logo
Title
c

cuddlecheek

05/22/2017, 2:51 PM
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

miha-x64

05/22/2017, 2:54 PM
cuddlecheek: I can share a working solution with you, but it is designed to work with Date & Calendar.
👍 1
c

cuddlecheek

05/22/2017, 2:56 PM
Oh, great, that would be awesome.
c

cuddlecheek

05/22/2017, 3:28 PM
Thank you, but I don't have a problem with counting the time, I'm struggling with the UI updating
m

miha-x64

05/22/2017, 3:39 PM
@cuddlecheek oh, quite misunderstood. This is a ViewHolder example, should call
bindTime
each `onBindViewHolder`:
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

cuddlecheek

05/22/2017, 4:52 PM
Thanks! your code helped me realize my silly mistake