G'day everyone, welcome back.
Now that we've created our AppDialog class, let's see how to confirm deletions in MainActivityFragment.
When the delete button's tapped, the onClickListener attached to the button in the cursorRecyclerViewAdapter class
calls MainActivityFragment's onDeleteClick function.
At the moment, that function uses the ViewModel's delete function to delete the task.
If we want to request confirmation before calling delete we can create an AppDialog instance,
and set the message string and button captions.
We'll start by creating a couple of constants, in MainActivityFragment.
It's possible that we may want to create more than one dialogue, and we'll need some way to identify them.
I mentioned this when we were creating the AppDialog class.
We'll use a constant for the delete dialogue, to make sure we're responding to the call back from the right dialogue.
We're also going to pass the task ID, to let us know which task should be deleted.
In the onDeleteClick function, we create a new AppDialog instance and show it.
Alright, what are we doing there?
Most of the code is creating the args bundle to pass data to the AppDialog instance.
We need to provide it with an ID because we wrote the class to need one, but you might still be unsure what it's for.
Don't worry, that will become clear when we add the code to respond to the dialog's callback.
We pass the message that the dialog will display, and also the text for the positive button.
The negative button defaults to cancel, which is why we're not setting it's text.
Before I continue, I'll fix the errors, starting with the delete on line 73.
Our AppDialog expects a string resource, not a string,
and we've got an error because I've provided a string to putInt.
Click on the delete text,
and use the light bulb to extract a string resource.
I'm gonna start the names for all this dialog's resources with the tag deldiag,
so we'll call this one deldiag_positive_caption.
Android studio tries to be helpful here, and uses the new resource ID in a call to get string.
We wrote our AppDialog class to expect resource IDs, not strings, so I'll edit the line to just pass the ID.
The line above is also showing an error.
The dollar symbol is used by Kotlin to indicate a variable value, and we don't have variables called d and s.
In a string resource file, that's the syntax for replaceable values,
and I've typed that line, intending to convert it to a string resource.
We talked about these back in section 9, the FlickrBrowser app.
So if you're not sure about them, review the video titled String Resources, currently lecture 164,
but the numbering changes if I insert additional videos and so forth,
so it's string resources in section 9 that you want.
Unfortunately, Android Studio won't convert this string to a string resource.
At the moment, we're going to have to edit the XML.
It will convert it, if we remove the placeholders first.
Now I can extract the string to a string resource,
which I'll call deldiag_message.
When I click OK the resource is added, and Android Studio puts in the getString function call for us.
All we have to do is add the additional arguments, the task ID and the task name, and modify the string resource.
In res/values/strings.xml,
we need to add the placeholders to the deldiag_message string.
That fixes the errors.
I'll stop this video here.
In the next video, I'll review what we've done, then we'll see if it works.
See you in the next one.