Welcome back.
In this video, it's time to actually save the values, so we prepared everything in the last video
and that was in this and happy place activity where we had this little to do left, right.
So when clicking on the save button, that's where we had to add all of the code in order to save the
values that the user entered.
But then before we do that, what we need to do is we need to check if the user entered the values or
not.
So if there are no values entered, then we don't want to store it.
So here for the title, we want to have a value for description for the date location and the image
as well.
So in order to check that, what can do is eat title.
So this are added text title we use text is null or empty.
So if that is the case.
Then let's inform the user about that, so let's just make a text here that's a little toast, letting
the user know that he should please enter something here.
OK, so I'm just going to use a toast here.
I'm just going to say.
Make text, please enter your title.
Now, this has to be surrounded with a when I hear so.
Otherwise, it's not going to work.
So here we say when.
The title is NULL or Empty.
Or the description is null or empty, so here Etty underscore description, if that is a. then do the
same thing.
So we're just going to copy this whole part here and please enter a description and so forth.
All right.
And then the next steps as well.
So the same for the location.
And here to underscore location.
And here, please enter a location.
Now, regarding the date, what I want to do is I want to give the user the date automatically.
So the date should be automatically assigned once the user opens the screen.
So we had this update in view, which we did, and our date.
Let's get listener here and what I'm going to do is I'm just going to call this method outside of the
states that listener as well.
So it's going to automatically populate the date with the current date.
All right, so that's why we don't need to specifically state something if the date is empty, because
it's not going to be empty anyways.
And then the next thing that could be is that the safe image to internal storage is not.
So if that's the case, then I want to inform the user that he should please select an image, so please
select an image and otherwise.
So if there is an image, if everything is fine, then I want to go ahead and execute my storage code.
So to save everything.
All right.
So first of all, I need to go ahead and create a happy place model object which will be of type happy
place model, of course.
And here we need to pass in all of the different values.
So, for example aside, I'm just going to assign zero because it's going to alter incremented anyway.
So we just passing the default value of zero, then I pass in the title.
So text to string.
That will be our title, of course, as you can see here.
Now the image strength is required.
So I'm going to use safe image to internal storage to string.
Next, we have the description.
So it underscored description.
Text to string the date, the location and so forth.
So I'm not going to bore you here, I'm just going to add to demand here, so here, of course, the
commas missing.
So the date, the location, the latitude and longitude.
And now that we have the happy place model, we can store it.
So let's do that.
Let's go ahead and create a DBI handler object, which will just be our database handler.
Which we call which is of type six letter database or was it called here?
Yeah, SQLite database, all right, and then.
We can go ahead and create a happy place.
Result.
So this will be the result, by the way, and the result, meaning if we look at our database handler,
we had this method called at Happy Place, which means that a happy place model object, but then it
returns a long value and we are going to store the long value in our at happy place variable here.
We could call this one at Happy Place Result or something like that, of course, as well.
But just going to go with this and then add happy place to be called with the happy place model that
we created.
OK, and now we can go ahead and check if.
The ad heavy plays is greater than zero, because if that's the case, then we know that there was no
error and things worked out.
And I'm just going to display a toast here, I'm just going to say make text their place details are
inserted successfully and after that we can.
Should finish the activity, so at that point, we would then come back to our main activity.
And of course, what's also nice if we is if we can check the database entries and I'm just going to
use this debug implementation here, I am told that the to be inspector or Kolon DB Inspector Kolon
3.0 for one at a R, which will automatically install an additional.
Application on our device, and then we can debug quite well.
So run the test again, so run the application again on the device, and now let's go over here and
create a new happy place.
I'm going to call this one, test three, because I already created multiple tests, test the description
three, even though it's typed incorrectly or written incorrectly, spelled.
So location, location, test three as well.
Had an image here.
I can either use the camera or the library.
Either way is fine.
So I'm just going to use the first angle that I'm getting.
As soon as the cameras loaded.
There we are.
So let's run or save this.
I selected Press safe.
All right, it's safe.
Now, let's go back to our phone here.
We can see a happy place database has been added there to table is called Happy Places Table.
And there we have our entry.
And in this case, we have three entries because I tested this already multiple times.
As you can see, test three is there.
We have the image.
A very long string here, the description to date, the location, latitude and longitude.
All right, cool.
So as you can see, it works.
So now, of course, we want to display this analyst view.
So despite all of our happy places and a list view that is nice and appealing so that we will get nothing
to do in the next few videos.
So see you there.