'How to delete audio file record and remove checked recyclerview item after user clicks 'Yes' on alert dialog

I have an alert dialog that pops up when the user clicks the delete button.

Alert dialog

When the user clicks yes, I want the recyclerview item they selected to be removed and delete the file from storage.

The onClickListener that shows the alert dialog

findViewById<ImageButton>(R.id.btnDelete).setOnClickListener{
        val mAlertDialog = AlertDialog.Builder(this@GalleryActivity)
        mAlertDialog.setIcon(R.drawable.ic_delete_dialog) //set alertdialog icon
        mAlertDialog.setTitle("Delete") //set alertdialog title
        mAlertDialog.setMessage("Delete audio?") //set alertdialog message
        mAlertDialog.setPositiveButton("Yes") { dialog, id ->

            //perform some tasks here
            Toast.makeText(this@GalleryActivity, "Deleted", Toast.LENGTH_SHORT).show()
        }
        mAlertDialog.setNegativeButton("No") { dialog, id ->
            //perform som tasks here
            Toast.makeText(this@GalleryActivity, "No", Toast.LENGTH_SHORT).show()
        }
        mAlertDialog.show()
    }


Solution 1:[1]

Some folks enjoy code to words, so I will do the two

Steps

  • So let assume you are getting your AudioRecord from local database or remote source, I believe you will already have a function or an endpoints to delete. now let assume the endpoint or database is collecting array of checked item. maybe array of the Id.

So in your

class DeleteRecordFragment: DialogFragment() {
  //Define an array collecting list of checked audio let assume
   private var checkedAudio : ArrayList<String> = arrayOf() 
 //Let assume your database to be   
   private var database =  MyDatabase()

as you check each item, you add the ID to > checkedAudio

now time to delete. inside Yes dialog. just call the function that delete and pass in the id of the array you are deleting

     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
     return activity?.let{ it ->
      val alertBuilder = AlertDialog.Builder(it)

    alertBuilder.setTitle("Delete record")
    alertBuilder.setMessage("Are you sure you want to delete?")

    alertBuilder.setPositiveButton("Yes", 
    DialogInterface.OnClickListener{ dialog, id ->
        Log.d("delete", "Yes Pressed")
   
     database.deleteSelectedItem(checkedAudio = checkedAudio)

    //Now fetch new list from the data source or database.


        database.fetchNewAudio()

     //for adapter to be aware to sync., you can call 


       mAdapter.notifyDataSetChanged()

     //but if you are using diffUtils, this should sync automatically
        }).setNegativeButton("Cancel", 
       DialogInterface.OnClickListener{dialog, id ->
        Log.d("delete", "Cancel Pressed")

       })
       alertBuilder.create()
      } ?: throw IllegalStateException("Exception !! Activity is null !!")
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Muraino