Android Apps for Absolute Beginners

Page 249

240

CHAPTER 10: Understanding Content Providers

After we do that, we can instantiate a new ContentValues object called myContact via the following declaration: ContentValues myContact = new ContentValues();

Immediately after that, we need to configure that object with a data pair via the put() method. This loads the ContentValues object with the table (People), the column (or field of data to operate on) NAME, and the string variable with the name in it, newName. myContact.put(People.NAME, newName);

Next, we use the getContentResolver() method to insert the myContact ContentValues object into the People table, which is at the location specified by CONTENT_URI constant we discussed earlier in the chapter: addUri = getContentResolver().insert(People.CONTENT_URI, myContact);

This writes the newName variable that we loaded into our myContact ContentValues object into the People.NAME database column that we specified in the same object. So, now our newName variable passed to our method has been taken care of, and we just need to do the same thing for our newNumber data variable. Then we will be finished. After this call, addUri will hold the location of the newly inserted record. The next line of code declares a Uri object named contentUri that appends the People.Phones.CONTENT_DIRECTORY onto the addUri and creates a new, more detailed URI object for the next query. (We are basically setting the location of where to add the phone number by using the location of the new name record as a reference.) Now all we need to do is change the data in the myContact ContentValues object for the final datainsertion operation. Uri contentUri = Uri.withAppendedPath(addUri, People.Phones.CONTENT_DIRECTORY);

The first thing we want to do to the myContact object is to clear it, or basically turn it into an empty object with a clean slate. Then, in the next two lines, we use the put() method to load the myContact ContentValues object with the URI and table and column values for the phone number field that we wish to write, and the newPhone phone number string variable data (415-555-7654), using the following lines of code: myContact.clear(); myContact.put(People.Phones.TYPE, People.TYPE_MOBILE); myContact.put(People.NUMBER, newPhone);

Finally, we call our powerhouse getContentResolver() method to go into our content provider and insert the phone number data into the correct table and data column (data field) location. This is done with the following code: changeUri = getContentResolver().insert(contentUri, myContact);

Once our data record is written by the two getContentResolver() operations, we can send a Toast to our users in the usual way, telling them that the write has been performed. Toast.makeText(this, "New Contact: " + newName + " " + newPhone, Toast.LENGTH_SHORT);

www.it-ebooks.info


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.