0

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.

nup
  • 11
  • 3

1 Answers1

0

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.

Zain
  • 13,030
  • 5
  • 26
  • 49
  • how do you add a listener to activity and pass to adapter whenever update list item? I have a hard time visuallizing that. – nup Apr 29 '21 at 14:10
  • Please check [this](https://stackoverflow.com/questions/65011597/save-items-of-checked-checkboxes-displayed-in-recycler-view-in-a-list/65011818#65011818) and [that](https://stackoverflow.com/questions/65266088/recyclerview-cardview-onclicklistener/65266166#65266166) – Zain Apr 29 '21 at 14:29