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