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 finish setting up our ContentProvider and finally see it in action!
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 just finished getting our cursor, and
0:00
now that we've got it let's loop through
it and add each contact to our list.
0:02
But first we'll need to make sure that
our cursor both exists and isn't empty.
0:07
So let's type if (cursor != to
null to make sure it exists,
0:12
and then add && cursor.moveToFirst
to make sure there is a first row.
0:19
MoveToFirst returns false if there
is not a first row to move to.
0:28
Inside the if statement we can use the do
while loop to loop through our cursor.
0:33
So do and then brackets.
0:39
And we'll leave the inside blank for
now and at the end add
0:43
while(cursor.moveToNext.
0:47
And add a semicolon.
0:54
This way, as long as there's
another row we'll keep looping.
0:56
Inside the loop, we just need to get
the ID and name of our contact and
1:01
then use that to create a new
text view for a linear layout.
1:06
Let's create a new string
variable named id and
1:10
set it equal to cursor.getString.
1:15
And then for the column index,
1:19
let's pass in Cursor.getColumnIndex.
1:23
And then that requires the column name, so
1:29
let's pass in DatabaseHelper.COLUMN_ID and
there we go.
1:32
And now that we've got our ID,
let's use command or
1:38
control D to duplicate this line.
1:41
And then let's just change
1:43
id to name and ID to NAME.
1:48
Then let's create a new TextView.
1:53
TextView textView = getNewTextView, and
1:57
parse in id and name, and
2:02
then let's add that text
view to our linear layout.
2:05
Linear Layout.addView textView.
2:10
And finally after the loop we need to
close our cursor, cursor.close and
2:16
that's it, we're now using a content
provider to serve up our data.
2:23
But before we run the app,
there's one more thing we should do.
2:27
Over in the manifest, we need to declare
our provider inside the application tags.
2:32
Let's add a couple lines at the bottom.
2:39
And then, add a provider tag and
let it auto complete.
2:42
Next, for the authorities, let's pass
in our authority, which remember,
2:47
is just the package name, plus provider.
2:51
So for me it's going to be
2:55
com.teamtreehouse.contentproviders.provi-
der.
2:57
And for the name,
let's give it our content provider.
3:04
Then let's close our provider tag and
run the app.
3:09
Looks good so far.
3:19
Now let's see what happens when we type in
a name like Stan and then hit the button.
3:21
Sweet.
3:28
It added the name to the list and we got a
toast of the URI where the name is stored.
3:29
Let's add another name to get
a better look at that URI.
3:35
Let's type Maddy and
then click the button.
3:38
And we've got the URI of the new row as
the content URI plus the ID of the row.
3:43
Awesome our content provider is
all set up, and it works great.
3:50
But, so
far it's only being used by one app, so
3:55
we're not getting much benefit
from our content provider.
3:58
In the next video we'll
create another app and
4:01
see how we can access our content
provider from the outside.
4:03
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