1 00:00:00,330 --> 00:00:02,540 We just finished getting our cursor, and 2 00:00:02,540 --> 00:00:07,158 now that we've got it let's loop through it and add each contact to our list. 3 00:00:07,158 --> 00:00:12,780 But first we'll need to make sure that our cursor both exists and isn't empty. 4 00:00:12,780 --> 00:00:19,913 So let's type if (cursor != to null to make sure it exists, 5 00:00:19,913 --> 00:00:28,292 and then add && cursor.moveToFirst to make sure there is a first row. 6 00:00:28,292 --> 00:00:33,654 MoveToFirst returns false if there is not a first row to move to. 7 00:00:33,654 --> 00:00:39,490 Inside the if statement we can use the do while loop to loop through our cursor. 8 00:00:39,490 --> 00:00:41,980 So do and then brackets. 9 00:00:43,990 --> 00:00:47,865 And we'll leave the inside blank for now and at the end add 10 00:00:47,865 --> 00:00:54,310 while(cursor.moveToNext. 11 00:00:54,310 --> 00:00:55,220 And add a semicolon. 12 00:00:56,800 --> 00:01:00,280 This way, as long as there's another row we'll keep looping. 13 00:01:01,450 --> 00:01:06,700 Inside the loop, we just need to get the ID and name of our contact and 14 00:01:06,700 --> 00:01:10,660 then use that to create a new text view for a linear layout. 15 00:01:10,660 --> 00:01:15,393 Let's create a new string variable named id and 16 00:01:15,393 --> 00:01:19,072 set it equal to cursor.getString. 17 00:01:19,072 --> 00:01:23,511 And then for the column index, 18 00:01:23,511 --> 00:01:29,680 let's pass in Cursor.getColumnIndex. 19 00:01:29,680 --> 00:01:32,370 And then that requires the column name, so 20 00:01:32,370 --> 00:01:37,350 let's pass in DatabaseHelper.COLUMN_ID and there we go. 21 00:01:38,680 --> 00:01:41,330 And now that we've got our ID, let's use command or 22 00:01:41,330 --> 00:01:43,810 control D to duplicate this line. 23 00:01:43,810 --> 00:01:48,704 And then let's just change 24 00:01:48,704 --> 00:01:53,190 id to name and ID to NAME. 25 00:01:53,190 --> 00:01:54,610 Then let's create a new TextView. 26 00:01:57,380 --> 00:02:02,290 TextView textView = getNewTextView, and 27 00:02:02,290 --> 00:02:05,075 parse in id and name, and 28 00:02:05,075 --> 00:02:10,923 then let's add that text view to our linear layout. 29 00:02:10,923 --> 00:02:15,240 Linear Layout.addView textView. 30 00:02:16,590 --> 00:02:23,840 And finally after the loop we need to close our cursor, cursor.close and 31 00:02:23,840 --> 00:02:27,920 that's it, we're now using a content provider to serve up our data. 32 00:02:27,920 --> 00:02:30,510 But before we run the app, there's one more thing we should do. 33 00:02:32,471 --> 00:02:39,250 Over in the manifest, we need to declare our provider inside the application tags. 34 00:02:39,250 --> 00:02:40,460 Let's add a couple lines at the bottom. 35 00:02:42,696 --> 00:02:45,526 And then, add a provider tag and let it auto complete. 36 00:02:47,345 --> 00:02:51,968 Next, for the authorities, let's pass in our authority, which remember, 37 00:02:51,968 --> 00:02:55,290 is just the package name, plus provider. 38 00:02:55,290 --> 00:02:57,058 So for me it's going to be 39 00:02:57,058 --> 00:03:04,150 com.teamtreehouse.contentproviders.provi- der. 40 00:03:04,150 --> 00:03:06,390 And for the name, let's give it our content provider. 41 00:03:09,580 --> 00:03:12,629 Then let's close our provider tag and run the app. 42 00:03:19,524 --> 00:03:21,310 Looks good so far. 43 00:03:21,310 --> 00:03:26,940 Now let's see what happens when we type in a name like Stan and then hit the button. 44 00:03:28,470 --> 00:03:29,230 Sweet. 45 00:03:29,230 --> 00:03:35,010 It added the name to the list and we got a toast of the URI where the name is stored. 46 00:03:35,010 --> 00:03:37,270 Let's add another name to get a better look at that URI. 47 00:03:38,660 --> 00:03:41,590 Let's type Maddy and then click the button. 48 00:03:43,340 --> 00:03:48,660 And we've got the URI of the new row as the content URI plus the ID of the row. 49 00:03:50,900 --> 00:03:55,000 Awesome our content provider is all set up, and it works great. 50 00:03:55,000 --> 00:03:58,120 But, so far it's only being used by one app, so 51 00:03:58,120 --> 00:04:01,090 we're not getting much benefit from our content provider. 52 00:04:01,090 --> 00:04:03,480 In the next video we'll create another app and 53 00:04:03,480 --> 00:04:06,480 see how we can access our content provider from the outside.