如何使用recyclerview MVVM删除房间数据库中的记录

How to delete a record in room database with recyclerview MVVM

我需要删除一个应该在房间数据库中通知的项目 recyclerview 适配器,请帮助我找到解决方案并提前致谢

 class ListAdapter : RecyclerView.Adapter<ListAdapter.MyViewHolder>() {
private lateinit var mitemsViewModel: ItemsViewModel
private var itemsList = emptyList<Item>()
private lateinit var item: Item
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
    return MyViewHolder(
        LayoutInflater.from(parent.context).inflate(R.layout.item_row_layout, parent, false)
    )
}
override fun getItemCount(): Int {
    return itemsList.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
    item = itemsList.get(position)
    //always remember this technique to save the values in val type
    val currentItem = itemsList[position]
    holder.itemView.itemNameTV.text = currentItem.itemName.toString()
    holder.itemView.itemCodeTV.text = currentItem.itemCode.toString()
    holder.itemView.itemCategoryTV.text = currentItem.itemCategory.toString()
    holder.itemView.itemDescriptionTV.text = currentItem.itemDescription.toString()
    holder.itemView.itemSellingPriceTV.text = currentItem.itemSellingPrice.toString()
    holder.itemView.itemStockTV.text = currentItem.itemStock.toString()
    holder.itemView.deleteItem.setOnClickListener {
        val itName = holder.itemView.itemNameTV.text.toString()
        val itCode = holder.itemView.itemCodeTV.text.toString()
        val itCategory = holder.itemView.itemCategoryTV.text.toString()
        val itDescription = holder.itemView.itemDescriptionTV.text.toString()
        val itSellingPrice = holder.itemView.itemSellingPriceTV.text.toString()
        val itStock = holder.itemView.itemStockTV.text.toString()
        val itime = Item(0, itName, itCode, itCategory, itSellingPrice, itStock, itDescription)
        mitemsViewModel.deleteItem(itime)
        //dao.deleteItem(itemsList.get(position))
    }
}
fun setData(item: List<Item>) {
    this.itemsList = item
    notifyDataSetChanged()

}}

帮助我如何在 recyclerview 适配器中初始化 ViewModel。 运行我的app

后的错误码
 kotlin.UninitializedPropertyAccessException: lateinit property mitemsViewModel has not been initialized
    at com.manju.mobilebilling.ui.items.ListAdapter.onBindViewHolder$lambda-0(ListAdapter.kt:65)
    at com.manju.mobilebilling.ui.items.ListAdapter.$r8$lambda$pJauI4KaymNCF6j043M3H3t3CwQ(ListAdapter.kt)
    at com.manju.mobilebilling.ui.items.ListAdapter$$ExternalSyntheticLambda0.onClick(D8$$SyntheticClass)
    at android.view.View.performClick(View.java:5651)
    at android.view.View$PerformClick.run(View.java:22445)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6138)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)

在 activity 中初始化视图模型,而不是通过适配器构造函数传递它

您应该在父 ActivityFragment:

中初始化 ItemsViewModel
private val viewModel by viewModels<ItemsViewModel>()

然后,而不是直接将其传递给 ListAdapter 声明一个自定义点击侦听器并将其用作参数:

// Add a parameter in the adapter
class ListAdapter(
    private val clickListener: ListClickListener
) : RecyclerView.Adapter<ListAdapter.MyViewHolder>() {

    private var itemsList = emptyList<Item>()
    private lateinit var item: Item

    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        return MyViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.item_row_layout, parent, false)
        )
    }

    override fun getItemCount(): Int {
        return itemsList.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        item = itemsList.get(position)
        //always remember this technique to save the values in val type
        val currentItem = itemsList[position]
        holder.itemView.itemNameTV.text = currentItem.itemName.toString()
        holder.itemView.itemCodeTV.text = currentItem.itemCode.toString()
        holder.itemView.itemCategoryTV.text = currentItem.itemCategory.toString()
        holder.itemView.itemDescriptionTV.text = currentItem.itemDescription.toString()
        holder.itemView.itemSellingPriceTV.text = currentItem.itemSellingPrice.toString()
        holder.itemView.itemStockTV.text = currentItem.itemStock.toString()
        holder.itemView.deleteItem.setOnClickListener {
            val itName = holder.itemView.itemNameTV.text.toString()
            val itCode = holder.itemView.itemCodeTV.text.toString()
            val itCategory = holder.itemView.itemCategoryTV.text.toString()
            val itDescription = holder.itemView.itemDescriptionTV.text.toString()
            val itSellingPrice = holder.itemView.itemSellingPriceTV.text.toString()
            val itStock = holder.itemView.itemStockTV.text.toString()
            val itime = Item(0, itName, itCode, itCategory, itSellingPrice, itStock, itDescription)
            // Call the click listener
            clickListener.onClick(iitem)
        }
    }

    fun setData(item: List<Item>) {
        this.itemsList = item
        notifyDataSetChanged()

    }
}

// Click listener class
class ListClickListener(val clickListener: (item: Item) -> Unit) {
    fun onClick(item: Item) = clickListener(item)
}

最后,在父级 ActivityFragment 中声明您的 ListAdapter

val adapter = ListAdapter(ListClickListener { item -> 
    viewModel.deleteItem(item)
})