Welcome back.
In the last year, we had this little book where we recreated or duplicated entries when we edited them,
so we kept the old country, but we created a new entry with the updated values.
So let's fix that problem.
First of all, we have to go to our database handler because that's where we handle all of our database
stuff.
And here we had to create an update and then we had this fun and happy place function.
And we're going to create a similar one, which I'm going to call update happy place.
Now, we could, of course, go ahead and create functions which are depending on a country that you
have here or a parameter.
But I think in this case, it really makes sense to have two functions, one for adding, the other
one for updating, even though they are so super similar to each other.
All right.
So first of all, we need a database that is rewritable.
So that stays the same then.
Everything else should also stay the same, except for this part where we insert something, so instead
of inserting something to the database, what we want to do is, of course, to the update something.
So let's create this variable called success and call DB Update.
So database, which is this writable database update.
And then we need to say what we want to update.
So I want to update the table called Happy Place with the content values.
Then we also need to add another value, which is our workflows.
So we want to update it on a specific entry.
So here.
I'm going to say at the point where the kid.
And here, key on the score, Edem.
Is equal to.
Our happy place.
Thought I'd.
And, of course, a plus missing, so I think it makes sense to put it in a new line here.
So we have this update method.
And there we pass in the.
Table the content values, and then we say where we want to change that, and that will be changed,
where the ID is, the happy place ID and we need to call the where arguments or person where arguments
here as well.
I'm going to pass and now.
OK, so in this case we are updating something, but only at one point.
And that is going to be where the ID is the same as the happy place ID that was passed when we call
the update Happy Place.
So we pass on an object of type happy place model, which contains, of course, all of the information
such as the title, the image and so forth.
And then we just go ahead and update all of that.
And now I'm just going to return success.
Now, the thing is, when updating, we're going to be update.
You can see it returns an integer.
But our update, happy place currently returns that long.
So we need to change that because when adding something you can see here, when inserting insert returns
a long and that's why it was totally fine to use a long here as the return type for our ad happy place
method.
But now when working with the update Happy Place Method, we're working with integers, so we need to
return an integer because the success here, which is the result of DB update, is an integer.
So when is that even relevant?
So when do we call either of those methods?
So either add or update.
Well, it's this save button here.
So once we.
Press on this button here at the bottom, so it's inside of adding this happy place.
So let's go to our happy place and we have this.
Button function, so it's inside of our unclick, so here.
When we pressed Button safe, we do all of this stuff and then we have a real happy place model, so
we called Debbie Handler at Happy Place.
So this is the spot where we should make changes, because now we don't only want to add happy places
all the time, but we really just want to do that if we create a new entry.
Otherwise we want to update a happy place.
All right.
So the changes start right here where the ID will either be zero or it will be the ID of happy place
details.
So here it's going to be if I'm happy.
Plus, details is now.
Then you zero else use em, happy place details, I'd.
OK, so what is this happy place these days?
Well, it's a happy place model object that we created here at the highest level and then we set that
one at one point.
So here, if we the intent has extra, that's where we get information about the happy place model that
we need to change.
So if that exists, then we want to update if it doesn't exist and we want to add a new entry.
OK, so we need to go back to.
That part here.
All right, so this is the if statement.
And now, finally, we need to check when calling dysfunction, so here we have the DB handler and now
either we call ad happy place or update happy place.
All right.
So here, let's create another if statement if I'm happy.
Place details is now.
Then just go ahead.
And to this quote here, so Rhonda at Happy Place and also check this one year.
So if statement to as you did before, but otherwise if it's not now.
Then go ahead and do the update, have your place so it's pretty much the same thing, only with difference
that it's now called Update Happy Place.
And of course, the function is also called update happy place.
And now we just need to pass in the model.
And of course, here, check if the result code.
Is great, greater zero, which means there is no error.
All right, so going back to our update, happy place.
You can see it returns an integer.
So if that integer that comes from the success or so from the attempts to update something, if that
is greater than zero, it means that things worked out.
And then we can go ahead and use it.
And the rest can stay the same.
OK, so this is pretty much it, what we need to check here.
So let's now test it again and see if the bug is fixed.
All right, so there we are.
Let's change test without anything, let's call this one test number one.
And press update, you could see now the first entry's test number one, and if I scroll, you see no
changes were made or no additions were made, no new entry was created.
Now, let's see if we create a new entry.
So test new entry test, new entry description.
Now, the location.
Somewhere.
Far away.
An image here.
And now we're safe and we can see this new entry, this new entry desk, so that worked perfectly.
All right, so that's already a pretty good step, you could see you could go ahead and create a to
do list now with all the knowledge that you have.
So you could use pretty much everything that you've learned so far and create a beautiful to do list
app, which would really do well, I think.
But then, of course, we want to add a bunch of new features which include the location so we will
see how to use Google Maps and everything else and the next few videos.
So see you in the next one.