[android-developers] [Android 2.0] New Contacts API

2009-11-05 Thread jefduncan
Hi all,

This is my first post to this group (any group), but I wanted to share
my experience with the new Contacts API so far. I had a little
application which accessed a contact's notes via
Contacts.People.CONTENT_URI, accessing the NOTES field with
cursor.getString(cursor.getColumnIndex(People.Notes)). Simple enough.

Then, along comes the new API with it's ContactsContract and
ContactsContract.CommonDataKinds.xxx. Oh my gosh! It took me forever
to figure out how to find the contents of a note. Maybe I'm just
dense! :) As it turns out, a note (along with many other data items)
is stored in the 'data' directory of the contact available through
ContactsContract.Data.CONTENT_URI. The note's mime-type is specified
as ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE, and the
column containing the note is
ContactsContract.CommonDataKinds.Note.NOTE. So, here's how I
constructed my new getNote(long contactId) method:

private String getNote(long contactId) {
Log.d(LOGGING_TAG, getNote for contactId:  + contactId);
String rv = null;
String[] projection = new String[]
{ ContactsContract.CommonDataKinds.Note.NOTE };
String whereClause = ContactsContract.Data.RAW_CONTACT_ID +  = ? AND
 + ContactsContract.Data.MIMETYPE +  = ?;
String[] whereParams = new String[]{Long.toString(contactId),
ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
Cursor c = getContentResolver().query
(ContactsContract.Data.CONTENT_URI, projection, whereClause,
whereParams, null);
if (c.moveToFirst()) {
rv = c.getString(0);
}
c.close();
Log.d(LOGGING_TAG, getNote returning  + rv);
return rv;
}

There are other, easier means for accessing some of the other 'data'
items (for example, ContactsContract.CommonDataKinds.Phone has it's
own CONTENT_URI), but this was the only way I could find to access a
note. I hope it's helpful to someone else.

Peace,
Jef

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] [Android 2.0] New Contacts API

2009-11-05 Thread Dmitri Plotnikov
Very nice!

It's important to clarify, that this code will find a note for a _raw_
contact, but not for an aggregate contact.  If the id here is contactId, not
rawContactId, then the selection should say ContactsContract.Data.CONTACT_ID
+ =?...

Cheers,
- Dmitri

On Thu, Nov 5, 2009 at 1:22 PM, jefduncan jef.dun...@topnotchgeek.comwrote:

 Hi all,

 This is my first post to this group (any group), but I wanted to share
 my experience with the new Contacts API so far. I had a little
 application which accessed a contact's notes via
 Contacts.People.CONTENT_URI, accessing the NOTES field with
 cursor.getString(cursor.getColumnIndex(People.Notes)). Simple enough.

 Then, along comes the new API with it's ContactsContract and
 ContactsContract.CommonDataKinds.xxx. Oh my gosh! It took me forever
 to figure out how to find the contents of a note. Maybe I'm just
 dense! :) As it turns out, a note (along with many other data items)
 is stored in the 'data' directory of the contact available through
 ContactsContract.Data.CONTENT_URI. The note's mime-type is specified
 as ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE, and the
 column containing the note is
 ContactsContract.CommonDataKinds.Note.NOTE. So, here's how I
 constructed my new getNote(long contactId) method:

 private String getNote(long contactId) {
Log.d(LOGGING_TAG, getNote for contactId:  + contactId);
String rv = null;
String[] projection = new String[]
 { ContactsContract.CommonDataKinds.Note.NOTE };
String whereClause = ContactsContract.Data.RAW_CONTACT_ID +  = ?
 AND
  + ContactsContract.Data.MIMETYPE +  = ?;
String[] whereParams = new String[]{Long.toString(contactId),
 ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
Cursor c = getContentResolver().query
 (ContactsContract.Data.CONTENT_URI, projection, whereClause,
 whereParams, null);
if (c.moveToFirst()) {
rv = c.getString(0);
}
c.close();
Log.d(LOGGING_TAG, getNote returning  + rv);
return rv;
 }

 There are other, easier means for accessing some of the other 'data'
 items (for example, ContactsContract.CommonDataKinds.Phone has it's
 own CONTENT_URI), but this was the only way I could find to access a
 note. I hope it's helpful to someone else.

 Peace,
 Jef

 --
 You received this message because you are subscribed to the Google
 Groups Android Developers group.
 To post to this group, send email to android-developers@googlegroups.com
 To unsubscribe from this group, send email to
 android-developers+unsubscr...@googlegroups.comandroid-developers%2bunsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/android-developers?hl=en


-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en