1
00:00:00,171 --> 00:00:02,532
(light piano music)

2
00:00:02,532 --> 00:00:05,360
(keyboard clicks)

3
00:00:05,360 --> 00:00:06,930
Alright, so at this point in time,

4
00:00:06,930 --> 00:00:09,250
we've entered the code for all of the functions

5
00:00:09,250 --> 00:00:10,577
in our content provider.

6
00:00:10,577 --> 00:00:12,210
And now it's time to test them

7
00:00:12,210 --> 00:00:14,260
to make sure that they actually work.

8
00:00:14,260 --> 00:00:15,550
So I'm back in main activity,

9
00:00:15,550 --> 00:00:17,470
I've opened it up, as you can see there.

10
00:00:17,470 --> 00:00:19,170
We left the on create function

11
00:00:19,170 --> 00:00:20,970
displaying a single record.

12
00:00:20,970 --> 00:00:23,500
So I am going to put it back to show all records.

13
00:00:23,500 --> 00:00:25,020
And I'll also take the opportunity

14
00:00:25,020 --> 00:00:27,040
to add all columns to the output.

15
00:00:27,040 --> 00:00:28,270
That way we can see what happens

16
00:00:28,270 --> 00:00:30,600
when we insert and update the data.

17
00:00:30,600 --> 00:00:32,229
Now the easiest way to do that

18
00:00:32,229 --> 00:00:34,670
is to pass null as the projection.

19
00:00:34,670 --> 00:00:36,870
If we don't specify which columns we want,

20
00:00:36,870 --> 00:00:37,710
we get all of them.

21
00:00:37,710 --> 00:00:39,880
So let's go ahead and make those changes.

22
00:00:39,880 --> 00:00:42,680
So I'm going to take a copy of this line on line 22,

23
00:00:42,680 --> 00:00:45,790
duplicate that, comment out the first one,

24
00:00:45,790 --> 00:00:48,333
should have done this the first time making the changes,

25
00:00:50,240 --> 00:00:54,397
the query Task contracts, dot, content URI, comma,

26
00:00:56,510 --> 00:00:58,270
projection as I mentioned, we've got to set that

27
00:00:58,270 --> 00:01:02,000
back to null, so we get all columns, then we need

28
00:01:02,000 --> 00:01:04,000
to just fix this up a little bit now

29
00:01:04,000 --> 00:01:05,650
and give us the ability to see

30
00:01:05,650 --> 00:01:07,950
all the columns in our database now.

31
00:01:07,950 --> 00:01:10,210
So I've uncommented that line,

32
00:01:10,210 --> 00:01:11,830
the next line, for now, we need

33
00:01:11,830 --> 00:01:13,510
to change the con index to one

34
00:01:15,130 --> 00:01:17,930
and uncomment the next one for description

35
00:01:17,930 --> 00:01:20,670
and change the sort order con index to three,

36
00:01:20,670 --> 00:01:23,620
and I also need to change result here,

37
00:01:23,620 --> 00:01:25,750
'cause I deleted some things out of that.

38
00:01:25,750 --> 00:01:29,340
So the first one is going to be ID, colon, dollar, ID

39
00:01:30,620 --> 00:01:32,917
A full stop at the name, dollar name,

40
00:01:33,940 --> 00:01:35,840
I'm going to put a full stop after that

41
00:01:35,840 --> 00:01:40,500
then we want description, colon, dollar description,

42
00:01:40,500 --> 00:01:42,470
a full stop after that, and for Sort order,

43
00:01:42,470 --> 00:01:44,407
I'm just gonna throw a capital S there for Sort order

44
00:01:44,407 --> 00:01:46,730
and put a dot on the end of that so it's clear.

45
00:01:46,730 --> 00:01:48,330
Alright, so let's just run this up again,

46
00:01:48,330 --> 00:01:50,340
and just make sure that it's still working,

47
00:01:50,340 --> 00:01:52,220
before we go through and start testing

48
00:01:52,220 --> 00:01:54,143
our content provider functions.

49
00:01:58,690 --> 00:02:01,670
You can see we've got our three entries here,

50
00:02:01,670 --> 00:02:05,660
working okay, and rows in returned cursor

51
00:02:05,660 --> 00:02:08,160
equals three, so that's all working nicely.

52
00:02:08,160 --> 00:02:09,090
And, as you can see there,

53
00:02:09,090 --> 00:02:10,669
they're appearing in sort order,

54
00:02:10,669 --> 00:02:14,663
that's because we left a sort column argument activated.

55
00:02:15,820 --> 00:02:16,830
We left that in there as well

56
00:02:16,830 --> 00:02:18,240
as that's being sorted correctly

57
00:02:18,240 --> 00:02:20,490
in this sort column order.

58
00:02:20,490 --> 00:02:22,690
Alright, so I've got a few tests to try,

59
00:02:22,690 --> 00:02:24,370
so what we need to do is firstly

60
00:02:24,370 --> 00:02:26,220
test inserting a new record,

61
00:02:26,220 --> 00:02:28,720
but then we need to test updating a single row,

62
00:02:28,720 --> 00:02:30,400
as well as multiple rows,

63
00:02:30,400 --> 00:02:33,830
and finally deleting a row and multiple rows as well.

64
00:02:33,830 --> 00:02:35,590
So, our column create function's gonna get

65
00:02:35,590 --> 00:02:37,920
a bit messy if we try and do all that in there.

66
00:02:37,920 --> 00:02:39,950
So what I'm gonna do is create test functions

67
00:02:39,950 --> 00:02:41,960
rather than putting all the code in oncreate.

68
00:02:41,960 --> 00:02:46,588
So, let's start by adding a test insert function,

69
00:02:46,588 --> 00:02:48,990
and I'm gonna do that just under the oncreate

70
00:02:48,990 --> 00:02:50,590
Now the code I'm about to write here,

71
00:02:50,590 --> 00:02:51,620
I'll start writing,

72
00:02:51,620 --> 00:02:54,650
is very similar to what we've actually used in the past,

73
00:02:54,650 --> 00:02:57,660
a private fun test insert, parentheses

74
00:02:59,650 --> 00:03:01,390
and we're gonna start by typing val,

75
00:03:01,390 --> 00:03:05,330
values is equal to content values,

76
00:03:05,330 --> 00:03:08,140
parentheses, dot apply.

77
00:03:08,140 --> 00:03:10,840
Then we get our left and right curly braces,

78
00:03:10,840 --> 00:03:11,990
and within there we're gonna type

79
00:03:11,990 --> 00:03:16,990
put in parentheses, task contract, dot columns,

80
00:03:18,000 --> 00:03:21,190
dot, this one's gonna be task and it's called name,

81
00:03:21,190 --> 00:03:24,820
comma, and in double quotes, new task one.

82
00:03:24,820 --> 00:03:26,490
That's gonna be the value of our new task

83
00:03:26,490 --> 00:03:27,910
for the task name,

84
00:03:27,910 --> 00:03:29,867
and we do the same for description of sort order

85
00:03:29,867 --> 00:03:33,970
so, put parentheses task contract

86
00:03:33,970 --> 00:03:37,930
dot columns dot task and it's called description

87
00:03:37,930 --> 00:03:39,210
comma and double quotes,

88
00:03:39,210 --> 00:03:40,803
we just type description one,

89
00:03:42,010 --> 00:03:44,230
then we'll also assign a sort order, as I mentioned,

90
00:03:44,230 --> 00:03:49,210
so, tasks contract in the put, dot columns, dot,

91
00:03:49,210 --> 00:03:51,220
task underscore sort underscore order,

92
00:03:51,220 --> 00:03:53,193
we're gonna set that to two.

93
00:03:54,720 --> 00:03:58,510
Okay, then, outside of the right curly brace,

94
00:03:58,510 --> 00:04:00,390
and I'll just move this up a little bit,

95
00:04:00,390 --> 00:04:03,980
we're going to type val uri is equal to

96
00:04:03,980 --> 00:04:06,923
content resolver dot insert,

97
00:04:06,923 --> 00:04:09,713
then in parentheses, we're gonna pass the uri,

98
00:04:09,713 --> 00:04:14,600
so task contract, dot content uri, comma values

99
00:04:16,510 --> 00:04:17,899
and then we're gonna do some logging,

100
00:04:17,899 --> 00:04:21,600
so log dot dee, parentheses, tag comma,

101
00:04:21,600 --> 00:04:25,280
then double quotes, new row id,

102
00:04:25,280 --> 00:04:26,613
in parentheses, in uri,

103
00:04:30,088 --> 00:04:33,530
that is dollar uri, so I can print that out.

104
00:04:33,530 --> 00:04:36,037
Then we'll also do another log id,

105
00:04:36,037 --> 00:04:37,260
and get the actual id, so,

106
00:04:37,260 --> 00:04:39,770
within parentheses is gonna be tag comma,

107
00:04:39,770 --> 00:04:44,770
double quotes, id in uri is and a top dollar sign

108
00:04:45,510 --> 00:04:46,620
and after write curly braces,

109
00:04:46,620 --> 00:04:49,883
it's gonna be task contract, dot get id,

110
00:04:51,304 --> 00:04:53,932
so calling the get id function, passing the uri

111
00:04:53,932 --> 00:04:58,313
so returning the ID back to see what that comes back as.

112
00:04:59,189 --> 00:05:01,800
Alright, so we needed a content values object

113
00:05:01,800 --> 00:05:03,200
to provide the new values that we're

114
00:05:03,200 --> 00:05:05,240
inserting into the database.

115
00:05:05,240 --> 00:05:09,260
And a content values object is very similar to a bundle

116
00:05:09,260 --> 00:05:11,900
so we use is put method, as you can see there

117
00:05:11,900 --> 00:05:14,850
to store key value pairs and here the key

118
00:05:14,850 --> 00:05:17,570
is the column name in the table that we're inserting into,

119
00:05:17,570 --> 00:05:20,280
and obviously the second argument is the actual data

120
00:05:20,280 --> 00:05:21,310
or in other words, what we wanna

121
00:05:21,310 --> 00:05:23,370
assign to that particular column,

122
00:05:23,370 --> 00:05:26,170
and we have to specify a value for the name column

123
00:05:26,170 --> 00:05:27,970
because we set that to not null

124
00:05:27,970 --> 00:05:29,942
when we created the tasks table,

125
00:05:29,942 --> 00:05:31,790
but technically you could have left off

126
00:05:31,790 --> 00:05:34,350
the description sort or order if we wanted

127
00:05:34,350 --> 00:05:36,990
because we didn't define them as being required.

128
00:05:36,990 --> 00:05:39,920
In other words, we didn't add not null

129
00:05:39,920 --> 00:05:42,260
when we actually specified those columns

130
00:05:42,260 --> 00:05:43,260
and thus, you can see, I've used

131
00:05:43,260 --> 00:05:46,640
unimaginative values for all three columns.

132
00:05:46,640 --> 00:05:50,100
Alright, so that's our function to do a test insert.

133
00:05:50,100 --> 00:05:53,040
Let's now add a call to that and I wanna create method

134
00:05:53,040 --> 00:05:54,520
and I'm going to call that prior

135
00:05:54,520 --> 00:05:56,200
to printing out the fields.

136
00:05:56,200 --> 00:05:58,990
Obviously, we can see where the dial is there

137
00:05:58,990 --> 00:06:00,470
so I'm gonna do a testInsert, a call

138
00:06:00,470 --> 00:06:02,960
to that function just after.

139
00:06:02,960 --> 00:06:04,870
Actually, what I'll do is I'll put it...

140
00:06:06,040 --> 00:06:08,720
Just after the setSupportActionBar line there.

141
00:06:08,720 --> 00:06:10,500
Let's call it there.

142
00:06:10,500 --> 00:06:12,473
And let's just run this then.

143
00:06:13,350 --> 00:06:16,643
And we should hopefully see four columns in our database.

144
00:06:19,256 --> 00:06:22,070
Alright, so before the records are displayed in the Logcat,

145
00:06:22,070 --> 00:06:24,020
there's the log entries for our insert method,

146
00:06:24,020 --> 00:06:25,370
as you can see up here.

147
00:06:25,370 --> 00:06:26,653
Insert called up here.

148
00:06:28,190 --> 00:06:30,410
Insert match is 100 and we've got a getinstance

149
00:06:30,410 --> 00:06:32,810
and then we've also got here the New row id

150
00:06:34,530 --> 00:06:35,500
showing there.

151
00:06:35,500 --> 00:06:38,740
So it's showing the actual uri with the id

152
00:06:38,740 --> 00:06:39,890
added to it on the left there,

153
00:06:39,890 --> 00:06:42,340
but on the next line, we actually got just the id.

154
00:06:42,340 --> 00:06:45,070
That was the call to the get id function

155
00:06:45,070 --> 00:06:46,770
returning just that number.

156
00:06:46,770 --> 00:06:48,013
And further on down, we can see

157
00:06:48,013 --> 00:06:49,709
that we've got our four entries there.

158
00:06:49,709 --> 00:06:51,730
We've got our new ones showing at the end there.

159
00:06:51,730 --> 00:06:53,670
Again, this is because it's sorted

160
00:06:53,670 --> 00:06:55,530
in sort column order.

161
00:06:55,530 --> 00:06:57,770
Sort column is set to sort order, I should say.

162
00:06:57,770 --> 00:06:59,060
So that's still showing correctly

163
00:06:59,060 --> 00:07:00,670
down the bottom of the screen there.

164
00:07:00,670 --> 00:07:02,030
So I've now got four rows showing

165
00:07:02,030 --> 00:07:04,980
so clearly the insert function worked fine.

166
00:07:04,980 --> 00:07:06,540
And that's really literally all there is

167
00:07:06,540 --> 00:07:10,060
to inserting records into a database table

168
00:07:10,060 --> 00:07:11,840
using a content provider.

169
00:07:11,840 --> 00:07:14,480
Now, it may have taken a bit of work creating the provider,

170
00:07:14,480 --> 00:07:15,550
but now that it's created,

171
00:07:15,550 --> 00:07:18,620
if we go back and check the code again,

172
00:07:18,620 --> 00:07:20,660
the task insert function there.

173
00:07:20,660 --> 00:07:22,720
So now that we've actually done that and created it,

174
00:07:22,720 --> 00:07:25,060
we don't have to mess around concatenating strings

175
00:07:25,060 --> 00:07:27,970
to build up SQL search statements, as you can see there.

176
00:07:27,970 --> 00:07:30,800
Now, you don't have to use a content provider

177
00:07:30,800 --> 00:07:32,620
and you saw in an earlier video

178
00:07:32,620 --> 00:07:34,690
how to use the SQL Exec method

179
00:07:34,690 --> 00:07:37,530
to run SQL commands against a database,

180
00:07:37,530 --> 00:07:40,270
but once the content provider class is written,

181
00:07:40,270 --> 00:07:43,090
using the database becomes far more straightforward

182
00:07:43,090 --> 00:07:45,080
but also less prone to the errors

183
00:07:45,080 --> 00:07:48,380
you can get when trying to build up SQL strings.

184
00:07:48,380 --> 00:07:50,090
Alright, so the values we've just inserted

185
00:07:50,090 --> 00:07:52,837
are a bit uninformative so let's see now

186
00:07:52,837 --> 00:07:54,830
how to update a row.

187
00:07:54,830 --> 00:07:57,770
So I'm gonna copy all the codes that inserted the new row

188
00:07:57,770 --> 00:07:59,470
from the test insert function

189
00:07:59,470 --> 00:08:02,080
and paste it into a new function called test update.

190
00:08:02,080 --> 00:08:04,530
So I'm gonna put that above here.

191
00:08:04,530 --> 00:08:09,413
So in private fun testUpdate, parentheses,

192
00:08:09,413 --> 00:08:11,340
open and closing code block.

193
00:08:11,340 --> 00:08:13,440
Let's just copy these values, all of them,

194
00:08:15,670 --> 00:08:18,003
paste that into the testUpdate function,

195
00:08:20,140 --> 00:08:21,680
and with a few modifications now,

196
00:08:21,680 --> 00:08:23,550
we can perform an update instead.

197
00:08:23,550 --> 00:08:24,550
So let's change this now.

198
00:08:24,550 --> 00:08:26,860
So the ContentValues instead of New Task 1,

199
00:08:26,860 --> 00:08:30,200
that's the value for the key value pair.

200
00:08:30,200 --> 00:08:33,440
We'll call this Content Provider.

201
00:08:33,440 --> 00:08:34,991
That's for the Task Name.

202
00:08:34,991 --> 00:08:36,929
And for the Task Description,

203
00:08:36,929 --> 00:08:40,602
let's change that to Record content providers videos.

204
00:08:42,865 --> 00:08:47,260
I'm going to delete the setting of the Task Sort Order,

205
00:08:47,260 --> 00:08:50,245
and then on the line with the uri,

206
00:08:50,245 --> 00:08:51,650
we're gonna change that a little bit.

207
00:08:51,650 --> 00:08:53,730
Instead of contentResolver.insert,

208
00:08:53,730 --> 00:08:55,060
we're doing an update so obviously,

209
00:08:55,060 --> 00:08:57,510
we need to call the update function

210
00:08:57,510 --> 00:09:00,640
and not the insert one and in terms

211
00:09:00,640 --> 00:09:01,580
of what we're passing to that,

212
00:09:01,580 --> 00:09:04,930
we've got TasksContract.Content_URI, which is still correct.

213
00:09:04,930 --> 00:09:06,720
Values is still correct, but we've got

214
00:09:06,720 --> 00:09:09,700
two other parameters we need to pass.

215
00:09:09,700 --> 00:09:13,630
Null, null, and that's the where

216
00:09:13,630 --> 00:09:14,720
and the selection arguments.

217
00:09:14,720 --> 00:09:16,270
We don't need to specify those.

218
00:09:17,280 --> 00:09:20,460
And I'm just going to comment out the two log entries there,

219
00:09:20,460 --> 00:09:22,000
which we don't need at the moment.

220
00:09:22,000 --> 00:09:26,470
Now the update function needs a uri and a set of values.

221
00:09:26,470 --> 00:09:28,227
At the moment, we're passing at the base uri,

222
00:09:28,227 --> 00:09:30,720
as you can see on line 56.

223
00:09:30,720 --> 00:09:34,660
We need one that includes an id to specify the ratio update.

224
00:09:34,660 --> 00:09:38,140
In this case, to update the row with id equals four

225
00:09:38,140 --> 00:09:40,880
and we're also getting back an int now

226
00:09:40,880 --> 00:09:43,840
from the call to the ContentResolver.Update

227
00:09:43,840 --> 00:09:46,300
rather than the uri so I'm gonna rename

228
00:09:46,300 --> 00:09:48,830
the uri variable for that reason as well.

229
00:09:48,830 --> 00:09:50,450
So what I'm gonna do is on the line before,

230
00:09:50,450 --> 00:09:55,450
I'm gonna type val_taskUri is equal to

231
00:09:55,595 --> 00:09:58,428
TasksContract.buildUriFromId four.

232
00:10:01,610 --> 00:10:03,487
So that's gonna be the id we're going to be updating.

233
00:10:03,487 --> 00:10:05,850
I'm gonna rename this uri 'cause we no longer

234
00:10:05,850 --> 00:10:07,690
have to mention getting uri back.

235
00:10:07,690 --> 00:10:09,647
We're getting the rows, number of rows set to reflected

236
00:10:09,647 --> 00:10:12,597
so I'm gonna call this, let's actually call it rowsAffected

237
00:10:13,940 --> 00:10:16,880
and that's gonna be a call to the contentResolver.update

238
00:10:16,880 --> 00:10:20,160
but we're also gonna pass the task uri here

239
00:10:20,160 --> 00:10:24,063
instead of passing TasksContract.Content_URI so taskURI

240
00:10:27,314 --> 00:10:28,957
and for values, and then the where

241
00:10:28,957 --> 00:10:31,330
and the selectionArgs can be null.

242
00:10:31,330 --> 00:10:33,050
And that's because we don't need to specify

243
00:10:33,050 --> 00:10:36,160
selection criteria when using an id, as we're using here,

244
00:10:36,160 --> 00:10:40,470
and now that we've set that up with that call on line 56,

245
00:10:40,470 --> 00:10:41,720
and that's the reason we pass null

246
00:10:41,720 --> 00:10:43,870
for those other two parameters.

247
00:10:43,870 --> 00:10:45,170
Alright, now that we've done that,

248
00:10:45,170 --> 00:10:47,900
we can actually log the number of rows

249
00:10:47,900 --> 00:10:50,700
that were affected by this update.

250
00:10:50,700 --> 00:10:53,403
So we'll do a Log.d parentheses Tag comma,

251
00:10:54,280 --> 00:10:57,060
double quotes, Number of rows updated

252
00:10:58,700 --> 00:11:01,073
is and dollar sign rowsAffected

253
00:11:03,880 --> 00:11:06,330
and I'll just change that to rowsAffected up here

254
00:11:07,690 --> 00:11:09,330
'cause it can be more than one.

255
00:11:09,330 --> 00:11:10,690
And then the last thing that we need to do

256
00:11:10,690 --> 00:11:13,240
is go into oncreate and we need to call

257
00:11:13,240 --> 00:11:16,790
this new test function instead of the test update.

258
00:11:16,790 --> 00:11:19,590
So I'm gonna comment out the testInsert

259
00:11:19,590 --> 00:11:23,060
and I'm gonna replace that with testUpdate,

260
00:11:23,060 --> 00:11:24,930
which is of course the function we just wrote.

261
00:11:24,930 --> 00:11:26,163
So let's now run this.

262
00:11:28,580 --> 00:11:29,633
And check our Logcat.

263
00:11:32,160 --> 00:11:34,320
Alright so you can see there that the Logcat's showing

264
00:11:34,320 --> 00:11:36,574
the number of rows updated is one.

265
00:11:36,574 --> 00:11:38,290
You can see that there.

266
00:11:38,290 --> 00:11:39,930
And row four, if you come down

267
00:11:39,930 --> 00:11:41,660
and have a look down the end here,

268
00:11:41,660 --> 00:11:43,420
you can now see that it's got

269
00:11:43,420 --> 00:11:45,950
the Task Name as Content Provider.

270
00:11:45,950 --> 00:11:49,080
Description now shows Record content providers videos

271
00:11:49,080 --> 00:11:53,270
and Sort order is unchanged, which is in this case correct.

272
00:11:53,270 --> 00:11:57,090
So updating a single row based on its underscore id

273
00:11:57,090 --> 00:11:59,730
is just as easy as inserting a new row

274
00:11:59,730 --> 00:12:01,610
when we use the content provider.

275
00:12:01,610 --> 00:12:02,840
You'll notice that we didn't have

276
00:12:02,840 --> 00:12:04,810
to provide values for every column.

277
00:12:04,810 --> 00:12:06,160
If you go back to the code.

278
00:12:07,690 --> 00:12:08,770
So obviously, in the case there,

279
00:12:08,770 --> 00:12:12,330
my test update function, we didn't provide values

280
00:12:12,330 --> 00:12:13,770
for the task sort order.

281
00:12:13,770 --> 00:12:15,640
Any columns that we don't provide values for

282
00:12:15,640 --> 00:12:18,047
will keep whatever value that everybody got

283
00:12:18,047 --> 00:12:20,290
and we saw that with the task sort order

284
00:12:20,290 --> 00:12:21,573
still being set to two.

285
00:12:23,170 --> 00:12:25,440
Alright, so let's finish the video here.

286
00:12:25,440 --> 00:12:27,350
In the next video, we'll continue on

287
00:12:27,350 --> 00:12:29,790
and we're gonna make a change to this routine,

288
00:12:29,790 --> 00:12:32,520
because at the moment we've got two entries now,

289
00:12:32,520 --> 00:12:34,980
as you can see here, that have got the sort order two

290
00:12:34,980 --> 00:12:38,460
and at this stage, we've only tested updating a single id.

291
00:12:38,460 --> 00:12:40,200
We wanna work on testing multiple,

292
00:12:40,200 --> 00:12:42,730
or updating I should say, multiple rows.

293
00:12:42,730 --> 00:12:44,730
So we'll work on that in the next video.

