How do I remove a row from recyclerview using room?

后端 未结 5 886
执笔经年
执笔经年 2020-12-03 23:09

I\'m trying to delete an row of recyclerview using room.im doing swipe to delete a particular row....

Here is my Address table-->

@Entity(tableName          


        
5条回答
  •  情深已故
    2020-12-04 00:03

    The issue is that you are only deleting the row in Kotlin/Java but not in your SQLite room DB. I guess you should be able to solve your problem by simply adding a line that deletes an address in your DB. here an example of how i would do this for your tvDelete clickable view:

    val application = application as CustomApplication
        
    
    viewHolder.tvDelete.setOnClickListener(View.OnClickListener { view ->
            mItemManger.removeShownLayouts(viewHolder.swipelayout)
            addresses.removeAt(position)
    
            //remember that in onBind you already saved the current Address as "fl"
    
            application.database.AddressDao().delete(fl)//here you delete from DB so its gone for good
            //notifyDataSetChanged() dont do this as it will reexecute onbindviewholder and skip a nice animation provided by android
            notifyItemRemoved(position)
            //notifyItemRemoved(position) execute only once
            notifyItemRangeChanged(position, addresses.size)//i dont think you will need this
            mItemManger.closeAllItems()
            Toast.makeText(
                view.context,
                "Deleted " + viewHolder.tv.getText().toString(),
                Toast.LENGTH_SHORT
            ).show()
        })
    

    so i guess you were quite close already. please mind that i code in java so im not super confident all my code is correct but you get the idea.

提交回复
热议问题