views:

1716

answers:

4

Hello! I'm using this code to retrieve all contact names and phone numbers:

        String[] projection = new String[]{
                People.NAME,
                People.NUMBER
             };

        Cursor c = ctx.getContentResolver().query(People.CONTENT_URI, projection, null, null, People.NAME + " ASC");
        c.moveToFirst();
        int nameCol = c.getColumnIndex(People.NAME);
        int numCol = c.getColumnIndex(People.NUMBER);

        int nContacts = c.getCount();
        do{
            //Do something
        } while(c.moveToNext());

However, this will only return the primary number for each contact, but i want to get the secondary numbers as well. How can i do this?

A: 

In the same way just get there other numbers using the other "People" references

People.TYPE_HOME
People.TYPE_MOBILE
People.TYPE_OTHER
People.TYPE_WORK
Donal Rafferty
+3  A: 

You can read all of the telephone numbers associated with a contact in the following manner:

Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, personId);
Uri phonesUri = Uri.withAppendedPath(personUri, People.Phones.CONTENT_DIRECTORY);
String[] proj = new String[] {Phones._ID, Phones.TYPE, Phones.NUMBER, Phones.LABEL}
Cursor cursor = contentResolver.query(phonesUri, proj, null, null, null);

Please note that this example (like yours) uses the deprecated contacts API. From eclair onwards this has been replaced with the ContactsContract API.

Jack Patmos
A: 

In case it helps, I've got an example that uses the ContactsContract API to first find a contact by name, then it iterates through the details looking for specific number types:

How to use ContactsContract to retrieve phone numbers and email addresses

Jay

AndroidRef.com
A: 

Is there any way to find the existing contact by passing the phone number instead of iterating through all the contacts and checking?

Deepa