Issue
So I have a scroll view and I'm loading images from firestore and then giving them to the recycler view. Then I'm getting the images from firebase storage and passing them into the adapater, once they reach I notify the adapter that the item's positoin has changed and that seems to work fine. The thing is that the update only works when I scroll, doesn't update automatically That's my adapter and udpate code
val adapter = RvMoviesToDeleteAdapter(tempArrayList)
adapter.setOnClickListener(object: RvMoviesToDeleteAdapter.OnClickListener {
override fun onClick(position: Int, movie: Movie) {
super.onClick(position, movie)
Log.e("a7a", "called 2")
openDeleteDialog(movie)
}
})
binding?.rvMoviesToDelete?.layoutManager = GridLayoutManager(requireActivity(), 3)
private fun updateImage(moviePosition: Int) {
CoroutineScope(Dispatchers.IO).launch {
Log.e("a7a", "called")
try {
SharedVars.allMoviesLoaded = movies
binding?.rvMoviesToDelete?.adapter?.notifyItemChanged(moviePosition)
} catch (e: Exception) {
handleLoadingErrors("updateImg", "$e")
}
}
}
I screen recorded my phone so you would better understand what I mean https://drive.google.com/file/d/1z4Kd3-CPbVs5IU-y2eSUekcvYvpBLhyA/view?usp=share_link
I tried adapter.notifyDataSetChanged()
and adapter.notifyItemChanged()
Solution
notifyItemChanged
is UI operation and must be triggered from the Main thread. Try moving your notifyItemChanged
inside withContext
like this:
try {
SharedVars.allMoviesLoaded = movies
withContext(Dispatchers.Main) {
binding?.rvMoviesToDelete?.adapter?.notifyItemChanged(moviePosition)
}
} catch (e: Exception) {
handleLoadingErrors("updateImg", "$e")
}
This should work.
Answered By - Ankur
Answer Checked By - David Goodson (JavaFixing Volunteer)