0

I'm trying to update my recyclerview but i don't really know how to tacle the issue, i can already print my list of data, for that i juste send my list to my adapter but i'd like to refresh it every 5 second. Someone know a solution? Here's my fragment thanks.

class DlFragment (
        private val context: MainActivity
    ): Fragment(){

override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
         ): View? {

    val view = inflater?.inflate(R.layout.fragment_dl,container, false)

    getDlList()

    val comfirmButton= view.findViewById<Button>(R.id.input_post_dl_button)
    comfirmButton.setOnClickListener { postDl(view) }

    return view
}

private fun getDlList() {
    GlobalScope.launch(Dispatchers.Main) {
        try {
            val response = ApiClientQnap.apiServiceQnap.getQuery(0,0,"all","all",ApiClientQnap.sid)

            if (response.isSuccessful && response.body() != null) {
                val content = response.body()
                if (content != null) {
                    //println(content)
                    val dlRecyclerView = view?.findViewById<RecyclerView>(R.id.dl_list_recycle_view)
                    dlRecyclerView?.adapter = DlAdapter(context, content.data)
                }
            } else { println("Error Occurred: ${response.message()}") }
        } catch (e: Exception) {println("Error Occurred: ${e.message}") }
    }
}

}

Bam Bou
  • 95
  • 1
  • 7
  • 1
    You can use the solution in this answer https://stackoverflow.com/a/4597788/4187541 – Peter Chaula Feb 15 '22 at 14:58
  • Thank it seem to work I put my getDlList() in the myTimer.schedule method but the recycleview scroll is reset every update, I think it's because the recycleview is recreated every loop. Maybe I just need to empty my array in the adapter and then load a new array but I cannot invoke an adapters methos in my fragment – Bam Bou Feb 15 '22 at 16:17

0 Answers0