简体   繁体   English

如何在kotlin中设置确认删除AlertDialogue框

[英]How to Set confirm delete AlertDialogue box in kotlin

I have finished building a note app following an old tutorial.我已经按照旧教程构建了一个笔记应用程序。 I want to set AlertDialogue box to confirm from the user if he really wish to delete a note.我想设置 AlertDialogue 框来确认用户是否真的想删除注释。

This is what i did that closes the App这就是我关闭应用程序所做的

 override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            //inflate layout row.xml
            var myView = layoutInflater.inflate(R.layout.row, null)
            val myNote = listNotesAdapter[position]
            myView.titleTv.text = myNote.nodeName
            myView.descTv.text = myNote.nodeDes
            //delete button click
            myView.deleteBtn.setOnClickListener {
                var dbManager = DbManager(this.context!!)
                val selectionArgs = arrayOf(myNote.nodeID.toString())
                dbManager.delete("ID=?", selectionArgs)
                LoadQuery("%")

                val builder = AlertDialog.Builder(this@MainActivity)
                builder.setMessage("Are you sure you want to Delete?")
                    .setCancelable(false)
                    .setPositiveButton(
                        "Yes",
                        DialogInterface.OnClickListener { dialog, id -> this@MainActivity.finish() })
                    .setNegativeButton("No", DialogInterface.OnClickListener { dialog, id -> dialog.cancel() })
                val alert = builder.create()
                alert.show()
            }

Please tell me what to do.请告诉我该怎么办。 thank you!谢谢你!

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(this@MainActivity)
    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()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM