Welcome back.
In the last year, we saw how we can add the entries to our second light database and now let's have
a look at how we can retrieve them.
And you already know how to do that if you followed along with the course.
But let's still do that in this video.
So where we are going to need that is in our main activity.
But where we are going to.
Pretty much created the function.
In order to get all the happy places is going to be in our database handler, so here we have the function
which creates the entry, but then let's create one function which will retrieve the entries.
So I'm going to call this function Get Happy Places list, and it will return an array list of happy
place models, so of our happy place there and now in there, I want to have the list.
So I'm going to create the happy place list.
Happy place, host of a realist.
Happy place model, which will be and the list of happy place models.
Of course, at that point, this would be redundant, so we already know that it's going to be a aerialists
of happy place models, but still.
As you can see, you can use both ways anyways.
All right, then we need to have the Querrey.
Do we want to run and to as we want to have all the entries, it's just going to be a very simple one.
So I'm just going to say select star from our table.
Happy place.
So we're just going to get all the entries from our table then.
We need to have a database that we can read from, so I'm just going to call it DBE, this DOT readable
database.
And then we will try to get the entries and of course, something can go wrong when you do that, so
try and catch is always a good idea here.
And I'm just going to add the catch plug here directly, which will potentially cause an SQLite exception.
And if that happens, I want to catch that and I want to.
Execute the sequel.
Or the select query that we created here.
And then just returned here and materialised.
OK, so if nothing went as we expected it and just returned and materialist.
And if everything worked, then I want to return my happy place list, so here, return a happy place.
Now, of course, this list will be empty at that point.
So we are currently returning an empty, happy place list.
But now what we want to do is, of course, to fill this list.
And what do we fill it with?
Well, we fill it with all of the entries that we have from this happy place model style that we have
here.
But of course, we are going to need them in here.
And we used to curser in order to go through every single entry that we have selected from our table.
All right.
So let's create this cursor, which will be of type cursor.
And here we need to import coercer.
All right, import it and then debe that rockery, so we run rockery, which is our select query query.
Then we also need to pass in the selection arguments, and I'm just going to pass a no, because that's
fine as well.
We don't have any specific options that we want to set here.
All right, and now we, first of all, move to the first entry of our curser, so the first vote,
so to speak, so move to first.
And if that works, then do the following, so run a do while loop.
All right, so do while there are still entries, so Curser Dot moved to next.
So this we say, OK, just run through the whole.
List of entries that you have, so for every single entry that you have in the database that came from
this select statement or the select query that we used here and then in here, what we do is we create
places.
So for every single entry, we create a new place.
So for every iteration of our.
So what I can do is I can create a new place which will be of type happy place model.
Now, of course, our happy place model needs a lot of information, as we can see here, needs to ID
need to title the image and so forth.
OK, so in order to get those, what we can do is we can read it from the cursor so cursor get it.
For example, if you want to get an integer cursor that gets column index and then you need to pass
in the column index, which was our ID.
And it has to be a string, of course, so in order to make sure that I'm using the right name, I go
here to underscore ideia case.
I have to be super careful here.
So either I use underscore or I do it correctly and I use the name of the column.
So our string that we created just for these purposes.
OK, so that is something that is super important to keep in mind because especially with strings,
things can go wrong.
All right.
So the first entry is we look at our ID here.
OK, the key, Edem, then the next one is going to be.
Our curser.
Get string, and here it's Curser thought, get Collum index had the key title and so forth.
OK, so we now need to do that for every single entry here.
And of course I was missing the bracket there.
So instead of me typing all of those cursor gets so-and-so, I'm just going to paste them in here.
So it's going to be all of those.
All right, so get string for the title, for the image, for the description, the date, the location,
and then get double for the latitude and longitude.
Hardened by this, we create a new place.
Now, what is left to do?
Well, what we want to return, we can see that here is going to be a happy place.
So what is this happy place list?
Well, it's a list of happy place models, so we just created a new happy place model.
Now let's at this place that we just created.
To our list, so here, happy list that ad plays.
All right, I know.
After all of that, we need to close our cursor so cursor dot close.
So that our program doesn't run into any errors and problems.
All right, so this is our helper method, which will allow us to get the place list now we need to
have one in our main activity, which does the same thing pretty much, but on a slightly different
level.
So in our main activity, I also want to have a function which gets all of those happy places.
So I'm going to create a new function here.
Private Fun Get Happy Places list from local database.
OK, so pretty long name, but that's it does it gets it from the local database, which is our Superflat.
So here we need to create an object of our DB handler in order to call this function.
OK, so this function that we just created, this happy place list.
All right, so in order to do so, value Debe Handler, so I'm going to call it, will be a data base
handler object and we need to pass in the context, which is this now we need to import database handler,
even though it's a class that we use ourselves, but it's not in the same.
Directory anymore.
So if you look at it, you could see here.
Database is in a different directory than our activities, than our main activity in this case, so
we need to import it and you can see that it says, OK, it's inside of happy places, database, database,
enter.
So here I get or I create a happy place, a list.
We're just going to be off type a realist happy place model.
And what do I get that from from our DB Hunter that get happy price list?
All right, so this is going to be our happy place list and it's going to be full already with all of
the entries.
So what we now need to do is we need to put all of those into a recycle of you because we want to display
them a recycle you later on.
But we don't have a recycle of UCLASS yet, so we will need to implement that later on.
But for now, what I'm going to do so I'm just going to write it to the log.
So I'm just going to say, if I get happy, place list is not empty.
So the size is bigger than zero or greater than zero.
If that's the case, then go through the whole list.
And give me all of the values, so in here, I'm just going to say log dot e and here, just give me
the title, which will be idot title.
And of course, we need to import a lot here.
There we are and now I'm going to keep it simple, I don't want to do that for all of the countries,
OK, so I'm just going to say the title and maybe also the description and this will be idot description.
So we're going through this for loop and I's going to be the happy place that we're currently looking
at.
And we just get the title of the happy place, currently looking at the standard description.
And of course, at one point we'll need to call this function and I'm just going to call it in the uncreate
method.
OK, so in there, I'm just going to call it straight at the start.
It should give me all of the happy places from the database.
All right.
At that point, we can test it.
So let's do that.
All right, so the activity started, let's look at our look and look for title and we can see title,
test title, test title and test three.
These were the three titles that I entered.
And we have the description and you can see we have all three descriptions, test description, test
description and then test the is June three.
OK, so all of them are there.
So now we can actually get the data from the database.
OK, so that's it.
In the next video, we are going to prepare to recycle you for the application to display all of that
data in a readable manner, because currently it doesn't.
Right.
So so you in the next one.