(light piano music)
(keyboard clicks)
Alright, so at this point in time,
we've entered the code for all of the functions
in our content provider.
And now it's time to test them
to make sure that they actually work.
So I'm back in main activity,
I've opened it up, as you can see there.
We left the on create function
displaying a single record.
So I am going to put it back to show all records.
And I'll also take the opportunity
to add all columns to the output.
That way we can see what happens
when we insert and update the data.
Now the easiest way to do that
is to pass null as the projection.
If we don't specify which columns we want,
we get all of them.
So let's go ahead and make those changes.
So I'm going to take a copy of this line on line 22,
duplicate that, comment out the first one,
should have done this the first time making the changes,
the query Task contracts, dot, content URI, comma,
projection as I mentioned, we've got to set that
back to null, so we get all columns, then we need
to just fix this up a little bit now
and give us the ability to see
all the columns in our database now.
So I've uncommented that line,
the next line, for now, we need
to change the con index to one
and uncomment the next one for description
and change the sort order con index to three,
and I also need to change result here,
'cause I deleted some things out of that.
So the first one is going to be ID, colon, dollar, ID
A full stop at the name, dollar name,
I'm going to put a full stop after that
then we want description, colon, dollar description,
a full stop after that, and for Sort order,
I'm just gonna throw a capital S there for Sort order
and put a dot on the end of that so it's clear.
Alright, so let's just run this up again,
and just make sure that it's still working,
before we go through and start testing
our content provider functions.
You can see we've got our three entries here,
working okay, and rows in returned cursor
equals three, so that's all working nicely.
And, as you can see there,
they're appearing in sort order,
that's because we left a sort column argument activated.
We left that in there as well
as that's being sorted correctly
in this sort column order.
Alright, so I've got a few tests to try,
so what we need to do is firstly
test inserting a new record,
but then we need to test updating a single row,
as well as multiple rows,
and finally deleting a row and multiple rows as well.
So, our column create function's gonna get
a bit messy if we try and do all that in there.
So what I'm gonna do is create test functions
rather than putting all the code in oncreate.
So, let's start by adding a test insert function,
and I'm gonna do that just under the oncreate
Now the code I'm about to write here,
I'll start writing,
is very similar to what we've actually used in the past,
a private fun test insert, parentheses
and we're gonna start by typing val,
values is equal to content values,
parentheses, dot apply.
Then we get our left and right curly braces,
and within there we're gonna type
put in parentheses, task contract, dot columns,
dot, this one's gonna be task and it's called name,
comma, and in double quotes, new task one.
That's gonna be the value of our new task
for the task name,
and we do the same for description of sort order
so, put parentheses task contract
dot columns dot task and it's called description
comma and double quotes,
we just type description one,
then we'll also assign a sort order, as I mentioned,
so, tasks contract in the put, dot columns, dot,
task underscore sort underscore order,
we're gonna set that to two.
Okay, then, outside of the right curly brace,
and I'll just move this up a little bit,
we're going to type val uri is equal to
content resolver dot insert,
then in parentheses, we're gonna pass the uri,
so task contract, dot content uri, comma values
and then we're gonna do some logging,
so log dot dee, parentheses, tag comma,
then double quotes, new row id,
in parentheses, in uri,
that is dollar uri, so I can print that out.
Then we'll also do another log id,
and get the actual id, so,
within parentheses is gonna be tag comma,
double quotes, id in uri is and a top dollar sign
and after write curly braces,
it's gonna be task contract, dot get id,
so calling the get id function, passing the uri
so returning the ID back to see what that comes back as.
Alright, so we needed a content values object
to provide the new values that we're
inserting into the database.
And a content values object is very similar to a bundle
so we use is put method, as you can see there
to store key value pairs and here the key
is the column name in the table that we're inserting into,
and obviously the second argument is the actual data
or in other words, what we wanna
assign to that particular column,
and we have to specify a value for the name column
because we set that to not null
when we created the tasks table,
but technically you could have left off
the description sort or order if we wanted
because we didn't define them as being required.
In other words, we didn't add not null
when we actually specified those columns
and thus, you can see, I've used
unimaginative values for all three columns.
Alright, so that's our function to do a test insert.
Let's now add a call to that and I wanna create method
and I'm going to call that prior
to printing out the fields.
Obviously, we can see where the dial is there
so I'm gonna do a testInsert, a call
to that function just after.
Actually, what I'll do is I'll put it...
Just after the setSupportActionBar line there.
Let's call it there.
And let's just run this then.
And we should hopefully see four columns in our database.
Alright, so before the records are displayed in the Logcat,
there's the log entries for our insert method,
as you can see up here.
Insert called up here.
Insert match is 100 and we've got a getinstance
and then we've also got here the New row id
showing there.
So it's showing the actual uri with the id
added to it on the left there,
but on the next line, we actually got just the id.
That was the call to the get id function
returning just that number.
And further on down, we can see
that we've got our four entries there.
We've got our new ones showing at the end there.
Again, this is because it's sorted
in sort column order.
Sort column is set to sort order, I should say.
So that's still showing correctly
down the bottom of the screen there.
So I've now got four rows showing
so clearly the insert function worked fine.
And that's really literally all there is
to inserting records into a database table
using a content provider.
Now, it may have taken a bit of work creating the provider,
but now that it's created,
if we go back and check the code again,
the task insert function there.
So now that we've actually done that and created it,
we don't have to mess around concatenating strings
to build up SQL search statements, as you can see there.
Now, you don't have to use a content provider
and you saw in an earlier video
how to use the SQL Exec method
to run SQL commands against a database,
but once the content provider class is written,
using the database becomes far more straightforward
but also less prone to the errors
you can get when trying to build up SQL strings.
Alright, so the values we've just inserted
are a bit uninformative so let's see now
how to update a row.
So I'm gonna copy all the codes that inserted the new row
from the test insert function
and paste it into a new function called test update.
So I'm gonna put that above here.
So in private fun testUpdate, parentheses,
open and closing code block.
Let's just copy these values, all of them,
paste that into the testUpdate function,
and with a few modifications now,
we can perform an update instead.
So let's change this now.
So the ContentValues instead of New Task 1,
that's the value for the key value pair.
We'll call this Content Provider.
That's for the Task Name.
And for the Task Description,
let's change that to Record content providers videos.
I'm going to delete the setting of the Task Sort Order,
and then on the line with the uri,
we're gonna change that a little bit.
Instead of contentResolver.insert,
we're doing an update so obviously,
we need to call the update function
and not the insert one and in terms
of what we're passing to that,
we've got TasksContract.Content_URI, which is still correct.
Values is still correct, but we've got
two other parameters we need to pass.
Null, null, and that's the where
and the selection arguments.
We don't need to specify those.
And I'm just going to comment out the two log entries there,
which we don't need at the moment.
Now the update function needs a uri and a set of values.
At the moment, we're passing at the base uri,
as you can see on line 56.
We need one that includes an id to specify the ratio update.
In this case, to update the row with id equals four
and we're also getting back an int now
from the call to the ContentResolver.Update
rather than the uri so I'm gonna rename
the uri variable for that reason as well.
So what I'm gonna do is on the line before,
I'm gonna type val_taskUri is equal to
TasksContract.buildUriFromId four.
So that's gonna be the id we're going to be updating.
I'm gonna rename this uri 'cause we no longer
have to mention getting uri back.
We're getting the rows, number of rows set to reflected
so I'm gonna call this, let's actually call it rowsAffected
and that's gonna be a call to the contentResolver.update
but we're also gonna pass the task uri here
instead of passing TasksContract.Content_URI so taskURI
and for values, and then the where
and the selectionArgs can be null.
And that's because we don't need to specify
selection criteria when using an id, as we're using here,
and now that we've set that up with that call on line 56,
and that's the reason we pass null
for those other two parameters.
Alright, now that we've done that,
we can actually log the number of rows
that were affected by this update.
So we'll do a Log.d parentheses Tag comma,
double quotes, Number of rows updated
is and dollar sign rowsAffected
and I'll just change that to rowsAffected up here
'cause it can be more than one.
And then the last thing that we need to do
is go into oncreate and we need to call
this new test function instead of the test update.
So I'm gonna comment out the testInsert
and I'm gonna replace that with testUpdate,
which is of course the function we just wrote.
So let's now run this.
And check our Logcat.
Alright so you can see there that the Logcat's showing
the number of rows updated is one.
You can see that there.
And row four, if you come down
and have a look down the end here,
you can now see that it's got
the Task Name as Content Provider.
Description now shows Record content providers videos
and Sort order is unchanged, which is in this case correct.
So updating a single row based on its underscore id
is just as easy as inserting a new row
when we use the content provider.
You'll notice that we didn't have
to provide values for every column.
If you go back to the code.
So obviously, in the case there,
my test update function, we didn't provide values
for the task sort order.
Any columns that we don't provide values for
will keep whatever value that everybody got
and we saw that with the task sort order
still being set to two.
Alright, so let's finish the video here.
In the next video, we'll continue on
and we're gonna make a change to this routine,
because at the moment we've got two entries now,
as you can see here, that have got the sort order two
and at this stage, we've only tested updating a single id.
We wanna work on testing multiple,
or updating I should say, multiple rows.
So we'll work on that in the next video.