Welcome back, and this year we are going to clean up our project a little bit, so we are going to
create separate directories and folders which will be needed because at this point we are going to use
models and databases and then we already have two activities.
So it makes sense to have different directories for each of them.
OK, so let's go ahead and create a new package here.
And the first one will be activities, then I create another one.
Which I'm going to call models.
And then a third one, which will be.
My database, so in this database directory, I'm going to put all the database, relevant classes that
we have and the activities, of course, all of my activity.
So let's drag them in there.
Let's refactor.
And what that does is it will create the right imports and it will adjust to imports accordingly, because
now our activities, they are not inside of our EU tutorials, happy places anymore, but they are on
a different level.
All right.
So the next thing that I want to create is a model because we are going to need a model for our happy
places.
Right?
So I'm going to call this one happy place model.
And I'm not calling it Happy Places model, because this is the model for every single individual happy
place.
All right, so let's go ahead and create that now, the happy place model will be a data class, so
it will not be a normal class.
It will just be a very simple data class in which I want to add the different variables that I want
to have each time that I'm creating a happy place.
So I want, for example, to have the ID.
So an a unique identifier then the title, which is some type of string.
Then I want to have an image which will be of type string.
And by this the image path is meant then a description, because if we look at it so we can check out
our application here we have the title description, date, location, the image and then also latitude
and longitude.
So we need to have the location in latitude and longitude as well, not just a string, which is the
address of the location, but both of them.
All right.
So then.
The image.
The description, so foul description, which is also type strength, the date, which is also type
string, the location.
After a string and then the latitude, which is of type double and the longitude, so latitude and longitude
are the two values with which you can specifically define a spot or find a spot on the earth.
All right.
Longitude will also be a double.
All right.
So this is our happy place model.
So each time we create a new happy place, we want to have all of this data.
And, of course, you might say, hey, we don't have location, we don't have latitude and we don't
have longitude.
Well, actually, location, we do have bits of string, right.
That we get from the edge of TextField.
But what we don't have is the longitude and latitude.
Well, we will need to create those words.
All right, the next thing I want is a new class here, and I'm going to create a database handler,
so it will be a school wide open helper class.
So I'm going to go ahead and create this class and I'm going to call this one database handler.
All right, and it should inherit from CircuLite open helper, so you might recall how to do that.
And by the way, this is something that I would ask you to try yourself.
So try to create a database helper or handler which will take care of the whole database stuff.
So we'll need to have a column for every single one of the values that we have for our happy places
model or happy place model.
So we need an ID column, we need to title column and image column, description column and so forth.
So we'll need columns for all of that.
The table name should be a happy place.
This table and the database name should be Happy Places database, OK, and everything else I think
you can already set up.
So try to do that to try to create the table.
So in the uncreate method, create a table and then add a function which will add a happy place.
So you will need to put all of the different values to the content values variable before then inserting
it to the database and closing the database.
OK, so please go ahead and try to do that in the database handler.
And if you don't know how to do that, then check out the demo on how to use databases.
Again, I know this is a tough one, but this one will be super helpful for you.
You will learn quite a bit while struggling, so to speak.
All right.
So good luck with that.
Alrighty, I hope you post the video and tried it, so I'm going to go over it.
So this is the database handler and as you might recall, we need to use Scollard Open Helper as the
class that we inherit from then that we need to pass in the context the database name, the factory,
which is just null and a database version.
All right.
And we need those variables as companion objects.
So I'm just going to use those variables here, as you can see, database version, database name.
And then we also need to have a name for the table.
And I'm going to call this one happy place, this table, and then all of the column names that we have.
And these column names are basically the ones that we have from our happy place model.
OK, so each time we create an object, we want to store that object inside of our database because
we cannot store objects as such.
We need to make sure that we have the variables of those objects in the right format, which we do here.
So we create all of those column names, which are all constants.
And then in the uncreate, that's where we create the happy place table.
So we create a new table.
Based on the name that we have in Table Happy Place, which is our happy place, this table, and then
we create the ID, which is our primary key of type in the title, which is just a text, the image,
which is also a text.
So strings can basically always be stored as text.
And you can see a key description as a text, key date as a text.
So basically what I did here is I just use texts here even for the latitude and longitude.
I also use text here.
You might have used real instead, but we're just going to use text here, OK?
And then we, of course, need to execute that signal light command, which is our create happy place
table command.
So it's this one containing all of that.
So at this point when this database handler is so the uncreate method is called, that's the point where
our table will be created and then in an upgrade, well, we just say, OK, drop table if they exist
and then.
Called the uncreate method, so nothing new here and then that happy place function, which will return
a long which basically will be the value if it succeeded or if it didn't succeed.
All right.
So what we're doing is we need a writable database first because we want to write something into the
database.
Then we have the content values to which we put of all our values here.
So we put the key title, computer key image in and so forth.
And here you get where we get them from.
So we get them from the happy place that is passed to us when we call this function.
So we want to have a happy place object that is passed to us, which contains all of this information
because it's a data class.
So we can create objects of it, of course, and passing all of those values in.
And then we can use all of those values to store them or for now to put them into the content values
variable here.
This one.
To then go ahead and insert that to the database, so we insert that to the table, which we call a
happy place, and of course, it has a different name, it called this called here Happy Place Table.
So that's a very specific name.
But the variable, of course, called Table Happy Place.
Then we pass and the column which has just said to null and the content values.
So that's the spot where we insert it to the database and this returns a result which is of type lunk.
So you can see here insert returns a long while.
This is a Java method here, but basically the same concepts apply and then we close the database and
we return the result.
So whatever the long result was.
All right, and now we can, of course, go ahead and save our data, because if we look at it here.
All of that stuff should be safe once we click on that button, so we need to add an on click listener
for that button and we need to run a method call, so to speak, when we click on that button.
So let's go to our activity where we handle that.
And that's our ad happy place activity.
And then in the uncreate method, that's where we added all of the.
On classlessness, so here I need a button on this cause, safe thought set on Click Listener.
And I'm going to pass on this, because this class at a happy place activity class will be our listener.
All right, and then, of course, we also need to do the same thing as we did with the other IDs,
here are dot, I.D. dot, and this time it's the.
But unsafe, so once we click on button safe, what do we want to do well, and here we want to store
or safe the data model to the database.
OK, that's what we're going to do later on.
But for now, we need to add something which we haven't added yet.
And these are variables that are important for our project to run because we need to pass them right.
We need to, for example, have an image which is going to be the URAI.
So we are not really passing the image itself, but we're just wanting the URAI.
So the location of the image, because it doesn't make sense to recreate the image and past the image
all the time, but it's make us a lot more sense to just passing the reference.
So I'm just going to create a new variable for this private variable called Save Image to internal storage.
And it's going to be of type URAI nullable.
So at the beginning, it's going to be not.
And then later on, we can assign a value there.
All right, then we, of course, need to have the latitude and longitude, so I'm just going to create
them private for and let me tud double, which will be zero zero and private four and longitude, which
will be also a double and 020.
So at this point, each time that we use the default values, we're going to be in the middle of the
Atlantic, somewhere west of Africa, not too far away from.
Africa, I'd say, OK.
So for now, let's just set a value for the safe image to eternal storage and the spots where it would
make sense to do so are going to be these ones here.
So here we had this variable called safe image to internal storage, but here it was a immutable variable
and it was one that is only visible in the scope of this on activity result.
But I, of course, don't only wanted to use in this try and catch block, but I want to store it permanently.
So I'm going to use while I'm just going to get rid of the valve.
Right.
So I use the exact same name.
So now I'm just going to store it at the moment when we get an image from the gallery.
And of course, the same thing when we get an image from taking a picture with a camera.
At this moment, of course, we are still not storing anything because we are never calling this database
handler method and happy place, as you can see, still grayed out.
But that's what we're going to do in the next videos.
So for now, I think it's enough for this video.
It's long enough in the next hour, we're going to add a little more spice dudes and we're going to
be able to store the data accordingly.
OK, so see you in the next one.