0 1 00:00:08,520 --> 00:00:14,080 So we've seen how to work with activities and how activities can interact between each other. 1 2 00:00:15,660 --> 00:00:24,330 Now we're going to look at content providers, earlier we mentioned how apps run in sandbox environments. 2 3 00:00:24,700 --> 00:00:32,160 A content provider allows an app to expose data to third party apps so taking the example we're working 3 4 00:00:32,160 --> 00:00:40,130 with say we are working with a shopping list and we want our app to be able to use this data but also 4 5 00:00:40,130 --> 00:00:45,970 share it with other apps that may want to access our shopping list. 5 6 00:00:45,990 --> 00:00:54,690 So what we want to do now let's change the UI text of the button and call it ADD item. 6 7 00:00:54,720 --> 00:01:03,530 Now what we want is when we click on Add item we want to add the text to a list. 7 8 00:01:03,580 --> 00:01:10,150 Now the code behind this is a bit complex and out of scope so we will be providing a lot of code that 8 9 00:01:10,150 --> 00:01:16,950 you will copy and paste but we'll just show you some of the steps involved in order for you to know 9 10 00:01:16,950 --> 00:01:25,650 what to look for in content providers. So the code itself if you're familiar with SQL well you will 10 11 00:01:25,650 --> 00:01:28,950 immediately see the similarities. 11 12 00:01:28,980 --> 00:01:31,020 So we created a database helper. 12 13 00:01:31,650 --> 00:01:41,630 We just do the basic stuff of creating a database inserting a row querying the database deleting an 13 14 00:01:41,630 --> 00:01:51,770 item updating an item standard SQL well stuff we will not go into the details of implementation but 14 15 00:01:51,770 --> 00:01:56,810 have a look at the code to see the format OK. 15 16 00:01:56,900 --> 00:02:00,330 So at the top we're defining a provider name. 16 17 00:02:00,380 --> 00:02:05,220 This is important since we'll be referring to it. 17 18 00:02:05,300 --> 00:02:12,120 Same thing with the URL which contains the provider name and this will be referred to within the code 18 19 00:02:12,210 --> 00:02:15,600 in the activities OK. 19 20 00:02:15,600 --> 00:02:22,430 Now we're going to modify what happens when we click on the button before we were taking the text and 20 21 00:02:22,430 --> 00:02:30,540 passing it on let's remove this or comment it out instead. 21 22 00:02:30,540 --> 00:02:37,020 Now we are going to say when the button is clicked we're not going to pass the text as a parameter to 22 23 00:02:37,020 --> 00:02:45,750 the other activity but instead we will add to the content provider so to do this we still want to get 23 24 00:02:45,750 --> 00:02:54,930 the initial part of defining the activity and getting the added text the different part however is if 24 25 00:02:54,930 --> 00:03:01,520 list item is not equal to null then content and values 25 26 00:03:03,910 --> 00:03:20,610 values equal new content values. Values dot put list provider dot list item and add list item, now define 26 27 00:03:20,640 --> 00:03:31,620 a URI of type URI and call get content resolver insert and tell it what URI we are 27 28 00:03:31,620 --> 00:03:36,530 passing to and pass the values OK. 28 29 00:03:36,650 --> 00:03:42,520 And just like that we added to the content provider and same as before. 29 30 00:03:42,530 --> 00:03:46,740 We want to start the activity. OK. 30 31 00:03:46,790 --> 00:03:54,420 Now in the display list items activity instead of receiving the value from the previous activity we 31 32 00:03:54,420 --> 00:03:58,620 want to obtain the list items from the content provider. 32 33 00:03:58,630 --> 00:04:05,840 Think of it as a database that we want to query so let's create a method called private String 33 34 00:04:05,860 --> 00:04:09,780 get list items. Inside 34 35 00:04:09,780 --> 00:04:17,190 we are going to say string URL equals and the name of our URL that we provided earlier. 35 36 00:04:18,980 --> 00:04:20,150 Then URI 36 37 00:04:20,150 --> 00:04:35,820 list items equal URI dot parse URL now to pass each item we use a cursor cursor C equals 37 38 00:04:36,240 --> 00:04:54,680 get content resolver DOT query and pass list items NULL NULL NULL AND ORDER BY list item. 38 39 00:04:54,690 --> 00:05:02,750 So this is similar to SQL here where we tell it what to sort by the other nulls are parameters that 39 40 00:05:02,750 --> 00:05:05,850 are out of scope. OK. 40 41 00:05:05,860 --> 00:05:14,780 Now define a new string called results declare as an empty string and for each of these we will say 41 42 00:05:15,740 --> 00:05:27,180 if C dot move to first do results equals, just copy this entire text. 42 43 00:05:27,260 --> 00:05:30,640 Here we are putting a query. 43 44 00:05:30,800 --> 00:05:34,230 Don't worry too much about the actual syntax. 44 45 00:05:34,340 --> 00:05:40,220 Just know that we're pointing to the items in our content provider OK. 45 46 00:05:40,230 --> 00:05:41,870 Then we return to results. 46 47 00:05:45,130 --> 00:05:49,960 So what are we doing in get list items. 47 48 00:05:50,030 --> 00:05:58,550 We are just pointing to our content provider listing all items and adding them to a string then we return 48 49 00:05:58,550 --> 00:06:01,630 the string. OK. 49 50 00:06:01,660 --> 00:06:09,850 Now in our on create instead of passing the item we just received from the previous activity we want 50 51 00:06:09,850 --> 00:06:13,180 to get the list items. 51 52 00:06:13,190 --> 00:06:20,880 This will also include the item that was just added to the list before opening this activity OK. 52 53 00:06:20,890 --> 00:06:24,250 Now an important thing before running this code. 53 54 00:06:24,640 --> 00:06:30,790 If you remember we said that all the Android components have to be listed in the manifest. 54 55 00:06:30,820 --> 00:06:41,190 We also need to define this content provider in the manifest so we just define a provider, Android colon 55 56 00:06:41,970 --> 00:06:57,690 authorities equals com dot flip cortex dot Hello World dot list provider Android colon name equals list 56 57 00:06:57,690 --> 00:07:11,580 provider. Close the provider and that is pretty much it. OK now we run ad item to list like milk ad item 57 58 00:07:12,910 --> 00:07:22,610 go back ad cheese and as you can see we are adding items to the shopping list again the idea here is 58 59 00:07:22,610 --> 00:07:29,720 just to demonstrate a use case of content providers we don't need to know the exact syntax of everything 59 60 00:07:29,870 --> 00:07:34,250 but it is important to know what a content provider is and what it might be doing 60 61 00:07:37,040 --> 00:07:38,690 if you did not manage to follow. 61 62 00:07:38,710 --> 00:07:44,150 Don't worry we'll be providing the finalized versions you can work with in the following parts.