Search code examples
androidandroid-fragmentskotlinandroid-fragmentactivity

Refreshing a Fragment in Kotlin


In My Project, I am deleting records in the recyclerview based upon the id. Delete is working fine but once data got deleted then fragment is not refreshing the layout. Any help is appreciated.

Code

deleteButton.setOnClickListener(this@ViewHolder)
override fun onClick(v: View?) {
    var mPosition:Int=adapterPosition
    var item:TestCart=mList[mPosition]

    when(v!!.id)
    {
        deleteButton.id ->
        {
            deleteItem(item.itemId!!)
            mList.removeAt(adapterPosition)
        }
    }

fun deleteItem(id: Int)
{

    var db: TestDatabaseHandler = TestDatabaseHandler(mContext)
    db.deleteItem(id)
}

Solution

  • Add notifyItemRemoved(adapterPosition) below mList and it should work.