This workshop will be retired on May 1, 2025.
Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we'll create the ContentProvider and see how we can query it!
Related Links
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We just finished looking over the app, and
0:00
now we're tasked with adding in
a middleman between us and our data.
0:02
This middleman of course
is the content provider.
0:06
So let's start by creating a new class.
0:09
And let's it call MyContentProvider.
0:14
Then let's make it extend
ContentProvider And
0:20
use Alt+Enter to implement
the six required methods.
0:26
Then let's use Alt+Enter a bunch
more to get rid of these warnings.
0:33
All right.
0:47
Now that we've got our content provider,
0:48
we need to turn it into a middleman
between us and our database.
0:50
Let's start by declaring a field for
the context.
0:54
Private Context, and
we'll call it context.
1:00
And then let's create another field for
our database.
1:08
private SQLiteDatabase, and
we'll call it database.
1:12
Then, inside the onCreate function,
let's add a line at the top,
1:20
and set our context field
equal to getContext.
1:27
Then, let's create a new database
helper object, DatabaseHelper,
1:32
and we'll call it databaseHelper
= new DatabaseHelper.
1:38
And we need to parse in our context.
1:43
Next, let's populate our
database field by typing
1:46
database =
databaseHelper.getWritableDatabase.
1:51
And finally, instead of returning false,
we should really be returning whether or
1:58
not the provider was started successfully.
2:04
So let's return database not equal null,
2:07
to return true if the database exists,
and false if it doesn't.
2:11
Awesome.
2:18
That's one function down, and five to go.
2:19
And actually, our app doesn't have
the ability to delete or update data.
2:21
So it's more like three to go.
2:27
But before we move on to the query
function, let's quickly talk about URIs.
2:29
URI, which stands for
Uniform Resource Identifier,
2:34
is pretty much just the address
of a resource in Android.
2:37
Just like how we use
URLs to access web pages,
2:42
we view URIs to access Android resources,
like content providers.
2:45
So each content provider has its own
URI that we can use to access it.
2:50
Getting back to the query function,
2:57
this function takes in
the parameters of a SQL query, and
2:58
should return a cursor containing the
results of that query, though currently,
3:02
we've had no idea what any of these
parameters mean, and it returns null.
3:06
So let's start by renaming these
parameters to make more sense.
3:12
Let's name the second one columns,
3:16
The third one selection,
3:22
the next one selectionArgs,
3:26
and then finally sortOrder.
3:30
And now, hopefully it's a little
easier to see what's going on here.
3:35
It's basically just
a standard SQL statement.
3:39
We select the columns from the URI, and
3:42
then the where clause is just the
selection combined with the selectionArgs,
3:46
by replacing any question marks in
the selection with the next argument.
3:51
Lastly, we can set the order by clause
by using the sortOrder parameter.
3:56
Moving on to the inside of the function,
4:02
we just need to return a cursor
containing the results of this query.
4:05
Let's start by creating a new SQL Lite
Query Builder Object, named builder.
4:09
SQLLiteQueryBuilder, and
we'll call it builder, and
4:15
set it equal to new SQLiteQueryBuilder.
4:19
Then we need to set which
table we're going to query
4:23
by calling the set table's method on our
builder and parsing in our VICS table.
4:26
So builder.setTables and parsing in our
VICS table, DatabaseHelper.TABLE VICS.
4:31
And finally,
4:41
we just need to return the result of
calling the query method on our builder.
4:42
So return builder.query,
which takes in a few parameters.
4:45
For the first parameter,
let's parse in our database,
4:53
which is just database, and
then the second parameter is projection N,
4:55
which is just another way of referring
to the columns we're selecting.
5:01
So let's just parse in columns, and
5:05
then let's parse in selection and
selectionArgs.
5:08
And now, if we like, we can add
a group by or having clause too.
5:13
But we won't be needing either of those,
so let's just parse in null and
5:18
null, and then let's finish up
by parsing in our sortOrder.
5:23
Nice, next step is the getType function.
5:29
This function returns the media type
of the content in the specified URI.
5:33
This way, other applications
can call our getType method
5:38
to see what kind of
content we are providing.
5:42
So if all our data
happened to be HTML code,
5:45
we might return something like text/html,
or
5:48
if it was all PNGs, maybe image/png.
5:53
And if we want,
we can even make up our own media types.
5:58
If you'd like to read
more about media types,
6:01
check out the teacher's notes below.
6:03
Also, if you don't need to tell other
applications what kind of content you're
6:06
providing, then you don't
really need to return anything.
6:10
So I'll just change this
back to return null, And
6:13
then the next video,
we'll start in on the insert function.
6:19
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up