8 - 4 - The ContentProvider Class - Part 2 (11-55)

5
[BLANK_AUDIO] Hi, I'm Adam Porter and this is Programming Mobile Applications for Android Handheld systems In addition to the query method, some common content resolver methods include: the delete, insert and update methods. The delete method takes three parameters, a U.R.I. specifying the data to be deleted, a string specifying a pattern for selecting which rows to delete. And another array of strings, providing arguments for that selection pattern. This method returns the number of rows that were deleted. The insert method takes a URI indicating the data set into which you want to insert a new row. It also takes an object of the content values type, which holds the fields for that new row. And this method returns the URI of the newly inserted row. And the update method also takes a URI and a content values object, just like Insert did. But like the delete method, it also takes a string selection pattern and an array of selection string arguments; for indicating which specific rows to update. And this method returns the number of rows that were updated Our next example application is called content provider insert contacts. This application reads several contacts from the Android contacts content provider. And it then inserts several new contacts into that content provider. And then displays the old and the new contacts. And finally, when the application exits, it deletes all these new contacts. Let's run this application. [SOUND]. So here's my device. And now I'll start the content provider insert contacts application. [BLANK_AUDIO] As you can see, this application is displaying a single button labeled Insert Contacts. I'll hit the Home button now and start the People application. Now here, you can see some contacts that I have on this device. Okay, now let me quit out of this

Transcript of 8 - 4 - The ContentProvider Class - Part 2 (11-55)

Page 1: 8 - 4 - The ContentProvider Class - Part 2 (11-55)

[BLANK_AUDIO]Hi, I'm Adam Porter and this isProgramming Mobile Applicationsfor Android Handheld systems In additionto the query method, some common contentresolvermethods include: the delete, insert andupdate methods.The delete method takes three parameters,a U.R.I. specifying the data to bedeleted, a string specifying a pattern forselecting which rows to delete.And another array of strings, providingarguments for that selection pattern.This method returns the number of rowsthat were deleted.The insert method takes a URI indicatingthe dataset into which you want to insert a newrow.It also takes an object of the contentvaluestype, which holds the fields for that newrow.And this method returns the URI of thenewly inserted row.And the update method also takes a URIand a content values object, just likeInsert did.But like the delete method, it also takesa string selection pattern and anarray of selection string arguments; forindicating which specific rows to update.And this method returns the number of rowsthat were updatedOur next example application is calledcontent provider insert contacts.This application reads several contactsfrom the Android contacts contentprovider.And it then inserts several new contactsinto that content provider.And then displays the old and the newcontacts.And finally, when the application exits,it deletes all these new contacts.Let's run this application.[SOUND].So here's my device.And now I'll start the content providerinsert contacts application.[BLANK_AUDIO]As you can see, this application isdisplaying a single button labeled InsertContacts.I'll hit the Home button now and start thePeople application.Now here, you can see some contacts that Ihave on this device.Okay, now let me quit out of this

Page 2: 8 - 4 - The ContentProvider Class - Part 2 (11-55)

application And now I'll restartour sample application, and again you cansee the Insert Contacts button.When I hit this button, some new contactswill be inserted into the contacts contentprovider.So, here it goes.And here you can see that I've insertedfour new contacts into the contactscontent provider.Now just to be sure let me hitthe home button again and restart thepeople application.Now as you can see, the application doesindeed Show thosenew contacts that we programmaticallyinserted into the contacts contentprovider.So that means that other applications cansee, display, and even modify these newcontacts.Let's look at the source code for thisapplication.So here's the application open in the IDE.Now I'll open the main activity.In OnCreate, the code first getsinformationabout the accounts registered on thisdevice.Next the code calls the insert all newcontacts method, which inserts the fournew contacts.And after this, the code sets up and usesaCursor Loader to get and display theappropriate contact information.Let's look at how this application insertsthe new contacts.The Insert All New Contacts method sets upa batch operation.In which it inserts all of the newcontacts at once.And so it starts by creating an array listof content provider operations.And it then calls the Add Record to Batchinsert operation method for each of thenew contacts.Now in this method, the code firstadds some information to the raw contactstable,such as the account's name and type, andthe fact that this contact should bestarred.Which means it's treated as a favoritecontact.Next, the method adds the new contact'sname to the data table.And after all the new contacts have beenadded to the batch operation, the insertall new contacts method then called the

Page 3: 8 - 4 - The ContentProvider Class - Part 2 (11-55)

appliedbatch method to commit the entire batchoperation.Finally, if you want to create your owncontentprovider, then you'll need to do thefollowing things.First, implement a storage system for thedata.And you'll often do this by creating anSQL Lite database,but as we'll see later, other approacheswill adjust just as well.Next, you 'll define what is called aContract Class.That defines constants and otherinformation that other applicationswill need in order to use your ContentProvider.After that, you going to define a ContentProvider subclass,implementing for example, methods such asdelete and insert.And finally, you'll declare and configureyour Content Provider.In the AndroidManifest.xml file for itsapplication.[BLANK_AUDIO]Our next example application is calledContent Provider Custom.This application defines a contentprovider for simple ID and string pairs.Let's first take a look at the source codefor this application.[BLANK_AUDIO]So here's the content provider customapplication open in the IDE.First I'll open up the content provideritself.And this is in the strings contentprovider.java file.Here you can see that this class extendsthe content provider class.[BLANK_AUDIO]Now first you can see that the actual dataisstored in a simple data structure of thetype SparseArray.So your content provider doesn't alwayshave to be implemented using a database.Next the code defines a number of methods.First, there's the delete method.Now, this method removes either a singlerecord fromthe database or it removes all of therecords.In addition this method doesn't processany of the SQL constructs.Now this method tests to see whether theURI.

Page 4: 8 - 4 - The ContentProvider Class - Part 2 (11-55)

That was passed in, refers to the wholetable or to a single record within thetable.And if it refers to the whole table, thenthe code clears the entire sparse array.And if it refers to only a single record,thenthe code removes that single record fromthe sparse array.[SOUND].And lastly the code returns the number ofrecords that were deleted.Next the code implements the get typemethod, this method uses the datacontract class to return the mime type ofthe data associated with agiven U.R.I. Next, the code implements theinsert method.This method extracts the data for a givenrecord, puts it in thedata record object, and then stores thedata record object in the sparse array.Finally, it returns a new URI that can beused to retrieve this new record.Next the code implements the query method.This method begins by creating a matrixcursor object, now this is just asimple cursor that will return all of thedata associated with each record.And continuing on, if the URI refers toall of the records, the code will insertevery record into this cursor If insteadthe URI refers to asingle record then the code will add onlythat record to the cursor.And finally the code returns the cursor itjust created.[BLANK_AUDIO]The last method is oncreate and normallyyou would initialize the data storagesystem here.But for this simple example, noinitialization is necessary, the codesimply returns true to indicate that theinitialization completed properly.And before we move on, let's open upthe Android manifest xml file for thisapplication.[BLANK_AUDIO]Here you can see that we've added aprovider tag.And within this tag, we've specified thenameof the class that implements this contentprovider.We've addedthe authority portion of the URI that willhave to be used to access this contentprovider.And we've set the exported attribute totrue

Page 5: 8 - 4 - The ContentProvider Class - Part 2 (11-55)

to allow other applications to access thiscontent provider.Our last example application is calledContentProviderCustomUser.This application is separate from thecontentprovider custom application that we justlooked at.However, it reads data from that contentprovider and displays the records in aListView.Let's look at this application in action.[BLANK_AUDIO]So here's my device.Now I'll start up the content providercustom user application.And as you see, the application displaystwo data records.Let's look at the source code for thisapplication to see where that data camefrom.[BLANK_AUDIO]So here's the content provider custom userapplication open in the IDE.Now I'll open up the main activity.The oncreate method begins by getting areference to the content resolver for thiscontext.Next, it creates a content values object.After that, the code adds a value forrecord one.And then calls the insert method to insertthat record into the content provider.The code then does the same thing for asecond, and a third record.And then, it deletes the first record thatit had previously inserted, by calling thedelete method.And after all of this, the code thenqueriesthe content provider to extract all of therecordsthat are currently stored in the contentprovider andthen it displays those records in a listview.[BLANK_AUDIO]So that's all for our lesson onContentProviders.Please join me next time for a discussionofthe last fundamental component of Android,the service class.Bye for now.[BLANK_AUDIO]