'I can't delete an entry using Onswipe. Using room, ViewModel

When I swipe, the record returns to its original place
Tell me how to use the ViewModel class to find a record but Id and delete it

My Fragment Code

private fun getSwapMg(): ItemTouchHelper{
    val viewModel = ViewModelProvider(this).get(ViewModelNotify::class.java)
    return ItemTouchHelper(object :ItemTouchHelper
    .SimpleCallback(0, ItemTouchHelper.LEFT){
        override fun onMove(
            recyclerView: RecyclerView,
            viewHolder: RecyclerView.ViewHolder,
            target: RecyclerView.ViewHolder
        ): Boolean {
            return false
        }

        override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {

            adapter.removeItem(viewHolder.adapterPosition)
            viewModel.delete(ModelNotify(viewHolder.adapterPosition))
        }
    })
}

ViewModel class

init{
    val wordsDao = DbNotify.getDatabase(application,viewModelScope).notifyDao()
    repository = RepositoryNotify(wordsDao)
    allNotifyList = repository.repositoryAllNotify
}

fun getAllNotify():LiveData<List<ModelNotify>>{
    return  repository.repositoryAllNotify
}

fun insert(notify:ModelNotify) = viewModelScope.launch {
    repository.insertNotify(notify)
}

fun delete(notify:ModelNotify) = viewModelScope.launch {
    repository.deleteNotify(notify)
}

}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source