○ 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: ○ Uri insert (Uri uri, ContentValues values) ● The update method ○ 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: ○ int update(Uri uri, ContentValues values, String where, String selectionArgs) Example: ContentProviderInsertContacts.
● This application reads several contacts from the Android contacts content provider, inserts several new contacts into the content provider and displays the old and the new contacts. ● When the application exits, it deletes all these new contacts. ● Initially, this application displays a single button labeled Insert Contacts. ● When I hit this button, some new contacts will be inserted into the contacts content provider. ● The screenshot below shows I've inserted four new contacts into the contacts content provider. ● These contacts will also be available to the People application (as long as this app is running). ○ Other applications can see, display, and even modify these new contacts. ● In onCreate , ○ The code first gets information about the accounts registered on this device. ○ Next the code calls the insertAllNewContacts method, which inserts the four new contacts. ○ After this, the code sets up and uses a CursorLoader to get and display the appropriate contact information. ● The insertAllNewContacts method ○ It sets up a batch operation in which it inserts all of the new contacts at once. ○ It starts by creating an ArrayList of content provider operations. ○ And it then calls the addRecordToBatchInsertOperation method for each of the new contacts. ● The addRecordToBatchInsertOperation method ○ The code first adds some information to the raw contacts table, such as the account's name, type and the fact that this contact should be starred, which means it's treated as a favorite contact. ○ Next, the method adds the new contact's name to the data table. ● And after all the new contacts have been added to the batch operation, the insert all new contacts method then called the applied batch method to commit the entire batch operation. Implementing ContentProviders ● If you want to create your own content provider, then you'll need to do the following things: 1. Implement a storage system for the data. You'll often do this by creating an SQL Lite database, but other approaches will work just as well.
You've reached the end of your free preview.
Want to read all 97 pages?
- Fall '16
- Object-Oriented Programming, Subroutine, intent, ISMG 4200, App Development