Im having a RatingBar in my fragment and i need to make it so that when user select any rating, it should update/add to the room db live. after doing so i will move the user to a new fragment. On navigating back i need to get that rating from db and show the user what rating value user has given, and then if user update that again above scenario will proceed.
how can i do this in android kotlin using room db? it will be much helpful if i can get an example. Im using MVVM approach.
The fragment
class CollectFragment : Fragment(), OptionSelectionListener {
private var ratingBar: RatingBar? = null
...
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
//enabling the transition while entering to this view
sharedElementEnterTransition = ChangeBounds().apply {
duration = 400
}
return inflater.inflate(R.layout.collect_layout, container, false)
}
...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navController = Navigation.findNavController(view)
ratingBar = view.findViewById(R.id.ratingBar)
observeChangesAndUpdateUI(view)
}
...
private fun observeChangesAndUpdateUI(view: View) {
ratingBar!!.setOnRatingBarChangeListener { p0, p1, p2 ->
Toast.makeText(
context,
"Given rating is: $p1",
Toast.LENGTH_SHORT
).show()
}
}
}
I would like if I had a workable example on this and much appreciated.