How to update check recyclerview list in room database?

740 Views Asked by At

So I have a to-do list, just the typical checkbox, and textview recycler view list. I want recycler view to remember the check state of all the checkbox in the list and when click Submit button, the list of checkstate of all checkbox in the list will be updated to a room database, but I've not been able to find a way.

These are some of the points I have found about the problem:

  • recycler view should not reference database (dao- viewmodel_
  • you can update in the onbindviewholder in adapter (though I don't understand how)

Thanks you.

1

There are 1 best solutions below

2
On

This is a potential pseudo guideline of doing this:

  • You need a model/pojo class that has a field to maintain the checkbox state, add the getters, & setters for it.

  • A list of this model class is passed from the activity/fragment to the RecyclerView adapter.

  • onBindViewHolder() should update the checkBox of each row from the list.

  • Whenever a checkbox is checked, you should update that on the corresponding list position, and a listener (which is implemented by the activity and passed to the adapter to trigger its callback whenever you update the list item (like changing the checkbox value).

  • When you click the save button, save the list in Room.

  • Whenever you show the activity, it should get the list from Room and pass it to the adapter.