views:

61

answers:

1

Hi I am trying to get a contacts first name. I know how to get a contacts full name, but I cannot figure out how obtain just the name. any ideas?

Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
    Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
    try {
       if (cur.moveToFirst()) {
        String name =  cur.getString(2);
        // do something with the name
       }
    } finally {
       cur.close();
    }
+1  A: 

I haven't personally tried this, but you may have to do a further RawContacts.CONTENT_URI query against the PersonLookup.LOOKUP_KEY from the first query, allowing you to retrieve the StructuredName.GIVEN_NAME field.

Christopher