I want to get some details from the contacts.
This is how I do it, contact selection works fine,
but getting the contact details works only for some of the contacts:


// ---------------- select one of the contacts 
-----------------------------------------------------------------
c = mContentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, 
null, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED 
ASC");
while(c.moveToNext()) // show all contacts
    
displayNames.add(c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
int index = ... // select one of the contacts
c.moveToPosition(index);
String contactId = 
c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
// ---------------- get given name 
-----------------------------------------------------------------
c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
    new 
String[]{ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME},
    ContactsContract.Data.CONTACT_ID + " = " + contactId, null, null);
if(c.moveToPosition(1))
String givenName = 
c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
// ---------------- get family name 
-----------------------------------------------------------------
c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
    new 
String[]{ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME},
    ContactsContract.Data.CONTACT_ID + " = " + contactId, null, null);
if(c.moveToPosition(1))
String familyName = 
c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
// ---------------- get company name 
-----------------------------------------------------------------
c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
    new String[]{ContactsContract.CommonDataKinds.Organization.COMPANY},
    ContactsContract.Data.CONTACT_ID + " = " + contactId, null, null);
if(c.moveToPosition(2))
String Company = 
c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY));
// ---------------- get mobile phone number 
------------------------------------------------------
c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
    new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, 
null, null);
if(c.moveToPosition(1))
String mobilePhoneNumber = 
c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// ---------------- get email address 
-----------------------------------------------------------------
c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
    new String[]{ContactsContract.CommonDataKinds.Email.ADDRESS},
    ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = "+ contactId, 
null, null);
if(c.moveToPosition(3))
String eMailAddress = 
c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));


Those "if(c.moveToPosition(...))" are strange, but seem necessary.
Any ideas to make this work reliably?

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

Reply via email to