Loading...

How to Set confirm delete AlertDialogue box in kotlin

Answer #1 100 %

I think here is the workflow that you want to achieve.

When users click on delete button, the app will show a confirmation dialog with format:

Message: Are you sure you want to Delete?
Action buttons: Yes, No

Yes: Delete the selected note from database
No: Dismiss the dialog

Here is the code

myView.deleteBtn.setOnClickListener {
    val builder = AlertDialog.Builder([email protected])
    builder.setMessage("Are you sure you want to Delete?")
        .setCancelable(false)
        .setPositiveButton("Yes") { dialog, id ->
            // Delete selected note from database
            var dbManager = DbManager(this.context!!)
            val selectionArgs = arrayOf(myNote.nodeID.toString())
            dbManager.delete("ID=?", selectionArgs)
            LoadQuery("%")
        }
        .setNegativeButton("No") { dialog, id ->
            // Dismiss the dialog
            dialog.dismiss()
        }
    val alert = builder.create()
    alert.show()
}

You’ll also like:


© 2023 CodeForDev.com -