Welcome back and this video, we are going to add to the lead feature so you can see here swiping our
list element to the left allows us to delete it.
So let's do that.
And you can see it's gone.
So this is something that I think would make sense as a little challenge.
So please try to implement that.
And of course, you will need to have another swipe to so-and-so callback.
So what makes sense to copy this class?
Pretty much and changed its name to swipe, to delete callback.
And then of course, the direction should not be right but left.
And the icon should be a different one.
And maybe the color should be a different one, but everything else should pretty much stay the same.
And then of course, you need to implement it at the right spots.
So please go ahead and try to do that.
All right, so I hope you tried it, and first of all, I'm just going to make it simple for the video's
purpose and I'm just going to copy this swap to delete callback.
And as I said, it's pretty much the same as the added callback.
So I'm going to paste it in here.
And as you can see now, the difference is that it uses this I see it with white, which is this white
icon, and of course, in order to use it, we need to change to.
Direction here or Directorate Turrill, happy places that are and now are will not complain anymore.
Now let's create this isolated white so file new vector assets and here let's use the bin.
And maybe the lead is the better key word.
So there we are to lead.
OK, and I'm going to use.
The following name like this, press next and finish.
OK, now this error disappeared and as you can see now the item Tuthill Baudot left is the simple callback
parameter here, or in this case, it's the argument.
And then the other thing that's different is the color.
So I'm using this reddish color here.
All right, the next thing is that we need to go to our main activity, because that's where there's
swiping the lead feature is going to be implemented and it's going to be pretty much the same thing
as we did with this added swipe handler.
So I'm just going to copy that and paste it down here.
And I'm going to call this one delete swipe handler.
And going to.
So I call here, swipe to delete callback, so create a new object of this.
And we need to import it.
Then on SWIP is going to work, that's the direction we're going to pass, is good as well.
Have a place adapter as a happy place adapter notify.
And here, maybe we should change this to notify the lead item and then here we don't have an added
item touch helper, but delete.
I don't touch Helper.
And of course, here we're not using the added swipe handler, but that that little swipe and then,
of course, as I said here, we should get rid of the entry and I'm going to do it here.
I'm going to say adapter that removes.
At.
And here I'm going to pass on a view holder dot adapter position.
Now, you might guess that remove ad is going to be a function that we created ourselves, so this notify
added item is the function that we created before to make changes to to edit something.
But now we actually want to remove something.
So let's go to our Happy Place adapter.
And here we're going to create a new function called Remove Fat, and we want to move at a specific
position.
All right, so here we are going to require the database handler in order to delete something in the
database as well.
So let's put this the context to this database handler that we create.
Then I'm going to.
Create a new variable, which I'm going to call is delete it.
Because what I'm trying to do is I'm trying to use DBI, Hendler Dot to lead a happy place at least.
Position now to lead Happy Place, of course, doesn't exist as well, so I need to create it in my
database handler so you can see the structure is always the same.
I'm doing it the other way around this time.
But basically what you need to do is you need to have a function in your adapter which allows you to
delete something.
So remove something from your adapter.
But then in order to delete it from the database, you need to create this in the database header.
So let's create a function in here, because here we get the happy place list, but we can also delete
a happy place.
I'm gonna call this one delete happy place.
And I need to say which happy place I want to delete.
So what I wouldn't do is I'm going to.
Create.
I writable database object this start writable database, and once I have this writable database, I
can go ahead and use the delete function, so I'm just going to store that in a variable called success,
which will be the success of the delete functionality here.
I can say which table I want to use to delete something or in which table I want to delete something.
And then the workless and I want to say please delete it where the key underscore ID is equal to our
happy place thought ID.
And of course, we need a bunch of plusses here in between, because I'm using a string concatenation
here.
OK.
All right, so then we also need to pass on the weather arguments and I'm just going to pass NUL here,
OK?
So this is the the leader functionality, so delete in order to delete something from the database and
then we can go ahead and say DBI CLO's return success.
So as you can see, we, of course, need to return an integer here, so here, return int.
All right, so this will delete it in the database, then this will remove it in the adapter and here,
of course, we still need to finish this.
Because we need to check now if it's deleted, that is greater than zero.
We know that everything worked out, so then we can go ahead and remove it at the position.
And then we need to notify our.
Adapter about item being deleted, so notify item removed that position.
OK, so remove that is a function that we create ourselves in our happy place adapter.
But the list that we get has a list which also has this function called remove it.
So we're deleting it from the array list here and we're notifying our adapter about an item being removed
so it will reload the whole recycle of you and the list will be updated, so to speak.
All right.
At that point, there's one thing left to do, and that is in our main activity.
We should finally get the happy place list from local database, so that's the function that we created
ourselves, which is this one here, private function, which is in the main activity, which gets all
of the entries in our database.
So what we're saying, OK, please check the database once again.
And that's what's happening here, and then it will, of course, populate the database with the current
list.
So let's say it's empty.
Well, then, of course, we want to display our TextView, which says no records available, as you
might recall.
All right, so let's test it.
Let's see if that's actually going to work.
So we have all our test entries and, well, everything seems just to work.
That's not what I wanted the lead.
And as you can see, it's deleting them from our list.
All right, so that seems to work.
And as you can also see, no happy places found yet kindly at one.
All right.
So let's restart our application.
Just see if it actually is gone.
So if it's actually empty and as you can see, still empty.
So now let's add an entry here.
Test one.
And the location is also going to be test one.
Now let's select the photo from the gallery.
And let's save it, and there we are.
All right, so now we have a edit and to delete feature both with a swipe gesture.
So that is pretty cool.
And I think now it's time to go ahead and add really new and really cool stuff.
But overall, I hope you were able to implement all of this functionality and get it done by yourself,
if not, no worries.
You still have some time to learn even more from the course.
But at one point, you really need to really get through it.
Just try to get things done by yourself, because that's when you really learn and when you have challenges.
That's exactly the spot where I wanted to have where you are challenged by whatever you're trying to
do.
So thanks a lot for being part of this class and yeah, let's go to the next video.
See you there.