To delete a task, create a new AsyncTask in the MainActivity:
private class DeleteTaskAsyncTask(private val database: AppDatabase?, private val selectedTask: Task) : AsyncTask<Void, Void, Unit>() {
override fun doInBackground(vararg params: Void): Unit? {
return database?.taskDao()?.deleteTask(selectedTask)
}
}
Next, in onOptionsItemSelected(), locate the following line of code:
dbHelper.deleteTask(selectedTask)
Replace it with this line of code:
DeleteTaskAsyncTask(database, selectedTask).execute()
Build and run. Select a task and click the Delete menu item. This removes the selected task from the ListView, removes it from the database as well, and displays a ticker with the message Task deleted successfully at the bottom of the screen:
That's it. As you can see, using the ORM lets you write less code and reduces SQL errors.