I'm trying to delete a note from a Room Database by calling a method when the user long clicks on the note, I set up the onLongClickListener on the RView adapter:
viewHolder.itemView.setOnCLickListener{
NotesFragment().deleteSingleNote(notesID)
}
On NotesFragment, the method looks like:
fun deleteSingleNote(notesID: Long) {
notesFragmentViewModel.deleteSingleNoteFromDB(notesID)
}
and on the ViewModel the method deletes the note through a coroutine.
The app crashes on long click, saying that the lateinit notesFragmentViewModel had not been initializing, pointing to the onLongCLickListener line, and NotesFragment line I pasted above.
The thing is, the notes are populated on the screen through a NotesFragmentViewModel LiveData variable which contains all the notes, so the ViewModel is very much initialized by the time I long click on the item.
I tried initializing the ViewModel on the adapter, but I got an exception related to ViewModel not being allowed tobe instantialized on something other than a fragment/activity.
This is my first post on SOverflow, so please let me know if I did anything wrong