Welcome back.
We're now ready to change our MainActivityFragment class
to restore the SwipeTask if the user cancels the deletion.
The RecyclerView will do most of the work.
All we have to do is call NotifyItemChanged
on our adapter.
The RecyclerView observes its adapter
and will redraw the item that was removed from the list.
We need to tell it which item was changed,
and we do that by passing the position
when we call NotifyItemChanged.
I'm going to write this code backwards.
Now I don't mean I'm going to be typing nuf instead of fun.
I'm going to assume we have the items position and write the code to use it.
We'll worry about getting the position once we've seen how we're going to use it.
I think it will be easier to understand that bit of code
if we've seen what we're trying to do.
We'll restore the item the DeletedTask
in the dialogues onNegativeDialogResult function
that's called when the user taps the dialog's cancel button.
I'll use control O to generate the function after
OnPositiveDialogResult.
On line 126,
we'll call the adapter's NotifyItemChanged function
passing in the position of the item that changed.
That's the position of the task that was swiped.
It should be obvious from line 123
that we're going to pass the position in the dialog's arguments.
The arguments are passed into this function in the args bundle.
We've seen this before a few times now.
I'll create the DialogTaskPosition constant
up with the others at the top of the file.
Okay. That's fixed the error. Of course, the code won't work yet.
We haven't added the position to the dialog's arguments.
The dialog is created in our OnDeleteClick function,
so that's where we'll add the position to the bundle.
The OnDeleteClick will get the position as an argument,
it then puts it into the bundle ready for us to retrieve
if the deletion is cancelled.
Now we're left with the problem of how to get the position.
Fortunately, that's easy.
It's an attribute of the ViewHolder
that's passed to the OnSwiped function.
Let's have a look at that function. Up on line 74,
we get past two arguments;
the ViewHolder of the item that was swiped
and the direction in which it was swiped.
Passing the ViewHoldersAdapter position to OnDeleteClick
will fix the error.
On line 78.
We get the position in OnSwiped
and we pass it to our OnDeleteClicked function.
That puts it into the dialog's bundle, so that we can retrieve it
in the OnNegativeDialogResult function and use it
when we call NotifyItemChanged.
The RecyclerView and RecycleRviewAdapter classes
were designed with this kind of use in mind,
which makes it very easy to implement swiping operations.
Let's see if it works. Run the application and swipe the task.
Notice that it disappears from the RecyclerView
before the dialog appears. That's how swiping works
but it's still in the database and it also still exists in the adapter.
We haven't deleted it yet.
It's only the RecyclerViewsDisplay that's changed.
Tap cancel
and the task reappears.
Okay. All that's left to do
is edit the task list items layout and delete
the delete button.
That will leave the tli edit button unconstrained
so constrain it to the right edge of the layout
with a margin of 8DP.
Its top constraint should be to the top of the layout
again with the margin of 8dp.
Clicking the plus icons in the constraint widget diagram on the right
is a quick way to create those constraints
but watch the margins and change them to 8 if another value is assigned.
Run the app again and test it thoroughly.
When you've found the bug your challenge is to fix it.
Here's a hint: try swiping to delete the task that's being edited
when you're in landscape mode.
I'll go over my solution in the next video. I'll see you there.