Android Tutorial

(avery) #1

By : Ketan Bhimani


336 

int phoneIDIdx = oneContact
.getColumnIndex(Contacts.People.PRIMARY_PHONE_ID);
oneContact.moveToFirst();
int emailID = oneContact.getInt(emailIDIdx);
int phoneID = oneContact.getInt(phoneIDIdx);


Now that we have the column index values for the contact’s name,
primary email address, and primary phone number, we need to
build the Uri objects associated with those pieces of information
and query for the primary email and primary phone number.
Uri emailUri = ContentUris.withAppendedId(
Contacts.ContactMethods.CONTENT_URI,
emailID);
Uri phoneUri = ContentUris.withAppendedId(
Contacts.Phones.CONTENT_URI, phoneID);
Cursor primaryEmail = managedQuery(emailUri,
new String[] {
Contacts.ContactMethods.DATA
},
null, null, null);
Cursor primaryNumber = managedQuery(phoneUri,
new String[] {
Contacts.Phones.NUMBER
},
null, null, null);

After retrieving the appropriate column indexes for a contact’s
specific email and phone number, we call
ContentUris.withAppendedId() to create the new Uri objects from
existing ones and the identifiers we now have. This enables direct
selection of a particular row from the table when the index of that
row is known. You can use a selection parameter to do this, as well.
Lastly, we used the two new Uri objects to perform two calls to
managed Query().

Now we take a shortcut with the requested columns String array
because each query only has one column:

String name = oneContact.getString(nameIdx);
primaryEmail.moveToFirst();

Free download pdf