A (175)

(Tuis.) #1
CHAPTER 14: Android Content Providers: Providing Data to Applications 545

What you just accomplished, in a very complicated nutshell, was create a ContentValues object to
hold your content values, load it with your electedViceroy name data, using the .put( ) method calls,
and load these content values into the RawContacts database using the getContentResolver( ).
insert( ) method call. Finally, you loaded all of that into the Uri object named newUri, which you are
about to convert into a different type of data value.


Now that you have a URI loaded with the RawContacts CONTENT_URI and Viceroy name String
data, declare a long variable named rawContactsId and load it with the result (using the equals
evaluator) of the ContentUris class’s .parseId( ) method call. You will pass the ContentUris.
parseId( ) method the newUri Uri object that you just created as its parameter. Finally, clear the
viceroyContact ContentValues object by calling the .clear( ) method off of it, which will empty all
of the data that you just loaded it with in the previous (first) four lines of Java code, and will thereby
clear it for its next use (during the next four lines of Java code).


Your Java code should look like the following structure, which is shown in Figure 14-40:


protected void addGalaxyViceroy(String electedViceroy) {
ContentValues viceroyContact = new ContentValues();
viceroyContact.put(RawContacts.ACCOUNT_NAME, electedViceroy);
viceroyContact.put(RawContacts.ACCOUNT_TYPE, electedViceroy);
Uri newUri = getContentResolver().insert(RawContacts.CONTENT_URI, viceroyContact);
long rawContactsId = ContentUris.parseId(newUri);
viceroyContact.clear();
}


Figure 14-40. Create a long variable named rawContactsId and set it equal to a result of ContentUris.parseId(newUri)

Free download pdf