1
00:00:00,620 --> 00:00:01,350
Welcome back.

2
00:00:01,609 --> 00:00:07,580
In the last year, we saw how we can add the entries to our second light database and now let's have

3
00:00:07,580 --> 00:00:09,700
a look at how we can retrieve them.

4
00:00:09,890 --> 00:00:13,280
And you already know how to do that if you followed along with the course.

5
00:00:13,280 --> 00:00:15,630
But let's still do that in this video.

6
00:00:15,650 --> 00:00:19,550
So where we are going to need that is in our main activity.

7
00:00:20,000 --> 00:00:21,710
But where we are going to.

8
00:00:22,700 --> 00:00:25,280
Pretty much created the function.

9
00:00:26,120 --> 00:00:32,479
In order to get all the happy places is going to be in our database handler, so here we have the function

10
00:00:32,479 --> 00:00:38,900
which creates the entry, but then let's create one function which will retrieve the entries.

11
00:00:40,300 --> 00:00:50,380
So I'm going to call this function Get Happy Places list, and it will return an array list of happy

12
00:00:50,380 --> 00:00:58,810
place models, so of our happy place there and now in there, I want to have the list.

13
00:00:58,990 --> 00:01:00,940
So I'm going to create the happy place list.

14
00:01:01,930 --> 00:01:06,220
Happy place, host of a realist.

15
00:01:07,780 --> 00:01:14,380
Happy place model, which will be and the list of happy place models.

16
00:01:15,920 --> 00:01:21,960
Of course, at that point, this would be redundant, so we already know that it's going to be a aerialists

17
00:01:21,980 --> 00:01:23,660
of happy place models, but still.

18
00:01:24,550 --> 00:01:27,340
As you can see, you can use both ways anyways.

19
00:01:27,730 --> 00:01:30,500
All right, then we need to have the Querrey.

20
00:01:30,500 --> 00:01:36,790
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.

21
00:01:36,790 --> 00:01:43,150
So I'm just going to say select star from our table.

22
00:01:43,960 --> 00:01:44,800
Happy place.

23
00:01:46,630 --> 00:01:50,800
So we're just going to get all the entries from our table then.

24
00:01:51,760 --> 00:01:57,790
We need to have a database that we can read from, so I'm just going to call it DBE, this DOT readable

25
00:01:57,790 --> 00:01:58,450
database.

26
00:01:59,690 --> 00:02:05,660
And then we will try to get the entries and of course, something can go wrong when you do that, so

27
00:02:05,840 --> 00:02:07,940
try and catch is always a good idea here.

28
00:02:08,180 --> 00:02:16,270
And I'm just going to add the catch plug here directly, which will potentially cause an SQLite exception.

29
00:02:19,350 --> 00:02:23,340
And if that happens, I want to catch that and I want to.

30
00:02:24,510 --> 00:02:27,180
Execute the sequel.

31
00:02:29,700 --> 00:02:31,770
Or the select query that we created here.

32
00:02:34,640 --> 00:02:37,940
And then just returned here and materialised.

33
00:02:38,510 --> 00:02:43,670
OK, so if nothing went as we expected it and just returned and materialist.

34
00:02:45,830 --> 00:02:52,640
And if everything worked, then I want to return my happy place list, so here, return a happy place.

35
00:02:53,120 --> 00:02:55,430
Now, of course, this list will be empty at that point.

36
00:02:55,440 --> 00:02:59,180
So we are currently returning an empty, happy place list.

37
00:02:59,340 --> 00:03:02,110
But now what we want to do is, of course, to fill this list.

38
00:03:02,420 --> 00:03:04,170
And what do we fill it with?

39
00:03:04,190 --> 00:03:10,130
Well, we fill it with all of the entries that we have from this happy place model style that we have

40
00:03:10,130 --> 00:03:10,460
here.

41
00:03:11,060 --> 00:03:14,960
But of course, we are going to need them in here.

42
00:03:15,620 --> 00:03:22,550
And we used to curser in order to go through every single entry that we have selected from our table.

43
00:03:23,090 --> 00:03:23,510
All right.

44
00:03:23,510 --> 00:03:27,920
So let's create this cursor, which will be of type cursor.

45
00:03:30,100 --> 00:03:32,050
And here we need to import coercer.

46
00:03:32,880 --> 00:03:40,890
All right, import it and then debe that rockery, so we run rockery, which is our select query query.

47
00:03:43,030 --> 00:03:48,310
Then we also need to pass in the selection arguments, and I'm just going to pass a no, because that's

48
00:03:48,310 --> 00:03:48,920
fine as well.

49
00:03:49,030 --> 00:03:52,750
We don't have any specific options that we want to set here.

50
00:03:54,890 --> 00:04:01,820
All right, and now we, first of all, move to the first entry of our curser, so the first vote,

51
00:04:01,970 --> 00:04:04,100
so to speak, so move to first.

52
00:04:06,150 --> 00:04:10,890
And if that works, then do the following, so run a do while loop.

53
00:04:11,750 --> 00:04:19,070
All right, so do while there are still entries, so Curser Dot moved to next.

54
00:04:20,329 --> 00:04:23,420
So this we say, OK, just run through the whole.

55
00:04:24,520 --> 00:04:31,390
List of entries that you have, so for every single entry that you have in the database that came from

56
00:04:31,390 --> 00:04:37,900
this select statement or the select query that we used here and then in here, what we do is we create

57
00:04:38,230 --> 00:04:38,800
places.

58
00:04:39,610 --> 00:04:42,870
So for every single entry, we create a new place.

59
00:04:43,180 --> 00:04:44,800
So for every iteration of our.

60
00:04:45,970 --> 00:04:52,100
So what I can do is I can create a new place which will be of type happy place model.

61
00:04:52,630 --> 00:04:58,990
Now, of course, our happy place model needs a lot of information, as we can see here, needs to ID

62
00:04:59,050 --> 00:05:02,110
need to title the image and so forth.

63
00:05:03,000 --> 00:05:08,890
OK, so in order to get those, what we can do is we can read it from the cursor so cursor get it.

64
00:05:09,410 --> 00:05:15,580
For example, if you want to get an integer cursor that gets column index and then you need to pass

65
00:05:15,580 --> 00:05:18,160
in the column index, which was our ID.

66
00:05:19,220 --> 00:05:25,430
And it has to be a string, of course, so in order to make sure that I'm using the right name, I go

67
00:05:25,430 --> 00:05:27,830
here to underscore ideia case.

68
00:05:27,830 --> 00:05:29,780
I have to be super careful here.

69
00:05:29,780 --> 00:05:34,970
So either I use underscore or I do it correctly and I use the name of the column.

70
00:05:35,520 --> 00:05:39,650
So our string that we created just for these purposes.

71
00:05:40,130 --> 00:05:47,180
OK, so that is something that is super important to keep in mind because especially with strings,

72
00:05:47,330 --> 00:05:48,410
things can go wrong.

73
00:05:48,440 --> 00:05:48,940
All right.

74
00:05:49,310 --> 00:05:53,540
So the first entry is we look at our ID here.

75
00:05:54,830 --> 00:05:59,270
OK, the key, Edem, then the next one is going to be.

76
00:06:00,630 --> 00:06:01,830
Our curser.

77
00:06:04,120 --> 00:06:14,200
Get string, and here it's Curser thought, get Collum index had the key title and so forth.

78
00:06:14,480 --> 00:06:18,420
OK, so we now need to do that for every single entry here.

79
00:06:19,030 --> 00:06:22,140
And of course I was missing the bracket there.

80
00:06:22,540 --> 00:06:29,860
So instead of me typing all of those cursor gets so-and-so, I'm just going to paste them in here.

81
00:06:31,540 --> 00:06:33,470
So it's going to be all of those.

82
00:06:34,090 --> 00:06:39,730
All right, so get string for the title, for the image, for the description, the date, the location,

83
00:06:39,730 --> 00:06:43,240
and then get double for the latitude and longitude.

84
00:06:44,270 --> 00:06:45,970
Hardened by this, we create a new place.

85
00:06:46,010 --> 00:06:47,090
Now, what is left to do?

86
00:06:47,510 --> 00:06:54,410
Well, what we want to return, we can see that here is going to be a happy place.

87
00:06:55,010 --> 00:06:56,600
So what is this happy place list?

88
00:06:57,820 --> 00:07:01,900
Well, it's a list of happy place models, so we just created a new happy place model.

89
00:07:01,930 --> 00:07:04,450
Now let's at this place that we just created.

90
00:07:05,720 --> 00:07:11,600
To our list, so here, happy list that ad plays.

91
00:07:14,330 --> 00:07:15,800
All right, I know.

92
00:07:17,190 --> 00:07:22,380
After all of that, we need to close our cursor so cursor dot close.

93
00:07:24,080 --> 00:07:27,680
So that our program doesn't run into any errors and problems.

94
00:07:31,130 --> 00:07:38,240
All right, so this is our helper method, which will allow us to get the place list now we need to

95
00:07:38,240 --> 00:07:44,270
have one in our main activity, which does the same thing pretty much, but on a slightly different

96
00:07:44,270 --> 00:07:44,580
level.

97
00:07:44,720 --> 00:07:50,980
So in our main activity, I also want to have a function which gets all of those happy places.

98
00:07:51,380 --> 00:07:52,940
So I'm going to create a new function here.

99
00:07:52,940 --> 00:07:59,300
Private Fun Get Happy Places list from local database.

100
00:08:01,160 --> 00:08:07,050
OK, so pretty long name, but that's it does it gets it from the local database, which is our Superflat.

101
00:08:07,370 --> 00:08:12,050
So here we need to create an object of our DB handler in order to call this function.

102
00:08:12,560 --> 00:08:17,660
OK, so this function that we just created, this happy place list.

103
00:08:19,490 --> 00:08:27,050
All right, so in order to do so, value Debe Handler, so I'm going to call it, will be a data base

104
00:08:27,500 --> 00:08:34,159
handler object and we need to pass in the context, which is this now we need to import database handler,

105
00:08:34,280 --> 00:08:38,150
even though it's a class that we use ourselves, but it's not in the same.

106
00:08:41,210 --> 00:08:42,150
Directory anymore.

107
00:08:42,500 --> 00:08:44,660
So if you look at it, you could see here.

108
00:08:46,030 --> 00:08:50,800
Database is in a different directory than our activities, than our main activity in this case, so

109
00:08:50,800 --> 00:08:57,890
we need to import it and you can see that it says, OK, it's inside of happy places, database, database,

110
00:08:57,940 --> 00:08:58,270
enter.

111
00:09:01,620 --> 00:09:07,860
So here I get or I create a happy place, a list.

112
00:09:10,890 --> 00:09:15,930
We're just going to be off type a realist happy place model.

113
00:09:19,040 --> 00:09:24,590
And what do I get that from from our DB Hunter that get happy price list?

114
00:09:27,510 --> 00:09:34,170
All right, so this is going to be our happy place list and it's going to be full already with all of

115
00:09:34,170 --> 00:09:34,770
the entries.

116
00:09:35,220 --> 00:09:42,510
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

117
00:09:42,510 --> 00:09:44,130
them a recycle you later on.

118
00:09:44,640 --> 00:09:48,990
But we don't have a recycle of UCLASS yet, so we will need to implement that later on.

119
00:09:49,230 --> 00:09:52,620
But for now, what I'm going to do so I'm just going to write it to the log.

120
00:09:53,070 --> 00:09:56,750
So I'm just going to say, if I get happy, place list is not empty.

121
00:09:57,360 --> 00:10:02,030
So the size is bigger than zero or greater than zero.

122
00:10:02,040 --> 00:10:05,460
If that's the case, then go through the whole list.

123
00:10:08,070 --> 00:10:17,070
And give me all of the values, so in here, I'm just going to say log dot e and here, just give me

124
00:10:17,070 --> 00:10:21,420
the title, which will be idot title.

125
00:10:22,780 --> 00:10:24,730
And of course, we need to import a lot here.

126
00:10:26,610 --> 00:10:31,850
There we are and now I'm going to keep it simple, I don't want to do that for all of the countries,

127
00:10:31,860 --> 00:10:38,420
OK, so I'm just going to say the title and maybe also the description and this will be idot description.

128
00:10:39,090 --> 00:10:44,430
So we're going through this for loop and I's going to be the happy place that we're currently looking

129
00:10:44,430 --> 00:10:44,700
at.

130
00:10:44,700 --> 00:10:49,080
And we just get the title of the happy place, currently looking at the standard description.

131
00:10:50,400 --> 00:10:55,620
And of course, at one point we'll need to call this function and I'm just going to call it in the uncreate

132
00:10:56,010 --> 00:10:56,450
method.

133
00:10:57,090 --> 00:11:00,060
OK, so in there, I'm just going to call it straight at the start.

134
00:11:00,310 --> 00:11:03,420
It should give me all of the happy places from the database.

135
00:11:04,650 --> 00:11:05,050
All right.

136
00:11:05,280 --> 00:11:06,480
At that point, we can test it.

137
00:11:06,690 --> 00:11:07,590
So let's do that.

138
00:11:10,250 --> 00:11:18,260
All right, so the activity started, let's look at our look and look for title and we can see title,

139
00:11:18,260 --> 00:11:20,900
test title, test title and test three.

140
00:11:21,300 --> 00:11:23,340
These were the three titles that I entered.

141
00:11:23,390 --> 00:11:28,820
And we have the description and you can see we have all three descriptions, test description, test

142
00:11:28,820 --> 00:11:33,550
description and then test the is June three.

143
00:11:34,170 --> 00:11:36,200
OK, so all of them are there.

144
00:11:36,290 --> 00:11:39,710
So now we can actually get the data from the database.

145
00:11:40,020 --> 00:11:41,150
OK, so that's it.

146
00:11:41,150 --> 00:11:47,150
In the next video, we are going to prepare to recycle you for the application to display all of that

147
00:11:47,150 --> 00:11:49,760
data in a readable manner, because currently it doesn't.

148
00:11:49,760 --> 00:11:50,060
Right.

149
00:11:50,450 --> 00:11:51,620
So so you in the next one.

