Updating a task

To update a task, create a new AsyncTask in the MainActivity:

private class UpdateTaskAsyncTask(private val database: AppDatabase?, private val selectedTask: Task) : AsyncTask<Void, Void, Unit>() {

override fun doInBackground(vararg params: Void): Unit? {
return database?.taskDao()?.updateTask(selectedTask)
}
}

Here, we make a call to the taskDao to insert the new task in the database in the doInBackground() method.

Next, in the onDialogPositiveClick() method, locate the following line of code: 

dbHelper.updateTask(todoListItems[selectedItem])

Replace it with this line of code:

UpdateTaskAsyncTask(database, todoListItems[selectedItem]).execute()

Further, in onOptionsItemSelected(), locate the following line of code:

dbHelper.updateTask(todoListItems[selectedItem])

And, replace it with this line of code:

UpdateTaskAsyncTask(database, todoListItems[selectedItem]).execute()

Now, build and run. Just like in the previous chapter, select a task and click the Edit menu item. In the Edit Task dialog that shows up, make changes to the task details and click on the SAVE button.

This takes away the dialog, saves the changes to the database, updates your ListView with the updated task, and displays a ticker with the message Task Updated Successfully at the bottom of the screen:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset