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