0

I have a list of elements (rv) and a function which deletes elements from adapter on swipe gesture. I also have an undo action. How to delete data from database? It deletes the wrong data.

MainActivity:

val onSwipe = object : OnSwipe(this) {
    override fun onSwiped(viewHolder: ViewHolder, direction: Int) {
        val cachedPosition = viewHolder.absoluteAdapterPosition
        val cachedItem = adapter.listArray[cachedPosition]
        when (direction) {
            ItemTouchHelper.RIGHT -> {
                adapter.removeItem(cachedPosition)

                val b =
                    dbManager.total().toBigDecimal().setScale(2, RoundingMode.HALF_UP)
                        .toString()
                binding.tvResult.text = "Сумма $b ₾"
                Snackbar.make(binding.rv, "Deleted", Snackbar.LENGTH_SHORT)
                    .setAction("Undo") { }
                        .addCallback(object: BaseTransientBottomBar.BaseCallback<Snackbar>() {
                            override fun onDismissed(transientBottomBar: Snackbar, event: Int) {
                                if (event == DISMISS_EVENT_ACTION) adapter.restoreItem(cachedPosition, cachedItem)
                                else adapter.deleteFromDB(dbManager, cachedPosition)
                            }
                        })
                            .show()

                    }
            }
        }
    }

Adapter functions:

fun removeItem(pos: Int) {
    listArray.removeAt(pos)
    notifyItemRemoved(pos)
    }

fun deleteFromDB (dbManager: DbManager, pos: Int){
    dbManager.removeItem(listArray[pos].id.toString())
}

    fun restoreItem(pos: Int, listMain: ListItem) {
        listArray.add(pos, listMain)
        notifyItemInserted(pos)
    }

I see 2 ways :

  1. Delete from database then from RV. But I will need to add data to database again when I use UNDO button. I don't know how to do that.

  2. Change function which deletes from database. How should it look?

Which is more correct? Database delete function:

fun removeItem(_id: String) {
    val id = BaseColumns._ID + "=$_id"
    db?.delete(DbColumns.TABLE_NAME, id, null)
}

OnSwipe class (shows red line and deleting icon):

abstract class OnSwipe (context: Context): ItemTouchHelper.SimpleCallback (0, ItemTouchHelper.RIGHT) {

    override fun onMove(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        target: RecyclerView.ViewHolder
    ): Boolean {
        return false
    }
    override fun onChildDraw(
        c: Canvas,
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        dX: Float,
        dY: Float,
        actionState: Int,
        isCurrentlyActive: Boolean
    ) {
        RecyclerViewSwipeDecorator.Builder(
            c,
            recyclerView,
            viewHolder,
            dX,
            dY,
            actionState,
            isCurrentlyActive
        )
           .addSwipeRightBackgroundColor(Color.RED)
           .addSwipeRightActionIcon(R.drawable.ic_baseline_delete_24)
            .create()
            .decorate()

        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
    }
}
user4157124
  • 2,809
  • 13
  • 27
  • 42
  • You're already using the code I gave you in your other question, where DB deletion only happens after the snackbar is dismissed without hitting undo. That's option 2. We have no idea how to delete from your database because we don't know how it's structured, but it probably involves using `cachedItem` to identify a record in the DB, like by an ID property – cactustictacs Nov 18 '22 at 22:43
  • I have a problem with this variant because I have a code which shows red color and an icon of deleting when a user swipes. So, the color and the icon don't disappear when a user presses undo without a code inside. But yes, this option works good for removing an item from DB and RV. I updated my question and added a function from DB and a code of my class which shows red color and an icon of deleting – Антон Кузнецов Nov 19 '22 at 13:51

0 Answers0