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
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
In this video we'll update MainActivity to use our ContentProvider instead of using the database directly!
This video doesn't have any notes.
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've finished our content provider and
now it's time to use it.
0:00
Over in main activity, Let's start by
0:03
clearing out our old database code
starting with our dbHelper field.
0:08
Then let's delete this
other dbHelper line.
0:14
And inside this if statement,
let's just delete dbHelper.
0:17
Leaving us with an unfinished
addContact function.
0:22
Then inside updateList, let's get rid of
everything except for the first line.
0:27
Awesome.
0:34
Now to finish cleaning up our code,
let's head over to DatabaseHelper and
0:36
delete the addContact and
getContacts method.
0:40
We can also delete the entire row class.
0:44
All right,
now let's get back to main activity and
0:56
start using our content provider.
0:58
Let's start inside our
buttons on ClickListener and
1:01
use Alt + Enter to create
a new addContact method.
1:04
And let's create it in MainActivity.
1:10
Inside this function, we're going to call
the insert method of our content provider.
1:15
Which means we're going to
need a content values object.
1:20
So let's start by creating a new
instance of content values.
1:23
ConstantValues we'll call it
values = new ContentValues.
1:28
Then, on the next line,
let's call values.put and then
1:35
we need to pass in the column name for
the key and our name for the parameter.
1:40
So let's pass in
DatabaseHelper.COLUMN_NAME,
1:45
and then our name parameter.
1:51
Nice.
1:54
Now that we've got our
content values object,
1:55
we just need to call the insert
method on our content provider.
1:58
And to do that,
we'll need to use a ContentResolver.
2:02
So on the next line,
2:06
let's get a ContentResolver
by typing getContentResolver.
2:07
And then let's call the insert function
which takes in two parameters.
2:11
A URI for
where we want to insert the data and
2:15
the data itself as
a content values object.
2:18
So let's pass in
MyContentProvider.CONTENT_URI for
2:23
the URI and pass in values for the values.
2:28
And nice, and
that takes care of inserting the data.
2:32
But just for fun, since we went
through the effort to make sure our
2:35
insert function returns a URI,
let's toast it.
2:38
Let's store the result of this
insert statement into a new URI.
2:42
Then, let's make sure our URI isn't null.
2:51
And if it's not, let's toast back
the URI of the newly inserted row.
2:59
Toast.makeText, pass in this for
the context,
3:03
and then uri.toString for the message.
3:09
And finally Toast.LENGTH_LONG, so
we have enough time to look at it.
3:14
Then let's call .show and groovy.
3:19
All that's left now is
the updateList function.
3:23
Inside this function, we're going to
use our content provider's query method
3:26
to get a cursor and then we'll loop
through that cursor to update the list.
3:30
Let's add a couple lines at the bottom and
3:35
then create a new cursor
variable named Cursor.
3:38
And let's set it =getContentResolver.query
which takes in the URI of
3:44
the data source and then a bunch of
parameters for each part of the SQL query.
3:49
So let's pass in
MyContentProvider.CONTENT_URI for the URI.
3:57
And then, since we're just
trying to select everything,
4:03
we can pass a null for
all four of the parameters we need.
4:06
So null, null, null, and null.
4:09
Cool.
4:16
In the next video, we'll finish
up with our content provider and
4:17
finally see it in action.
4:20
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