import android.content.DialogInterface
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Create a button click listener to show the delete alert
val deleteButton = findViewById<Button>(R.id.deleteButton)
deleteButton.setOnClickListener {
showDeleteAlert()
}
}
private fun showDeleteAlert() {
// Create an AlertDialog.Builder
val builder = AlertDialog.Builder(this)
// Set the alert dialog title, message, and buttons
builder.setTitle("Delete Confirmation")
.setMessage("Are you sure you want to delete this item?")
.setPositiveButton("Yes") { dialog, which ->
// User clicked Yes, perform delete operation here
performDeleteOperation()
}
.setNegativeButton("No") { dialog, which ->
// User clicked No, do nothing
}
// Create and show the AlertDialog
val alertDialog: AlertDialog = builder.create()
alertDialog.show()
}
private fun performDeleteOperation() {
// Implement the logic to delete the item here
// This is where you would perform the actual deletion from your data source
// For example, you might delete a record from a database or remove an item from a list
}
}