0

I've found this solution to loop contacts:

Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    while(people.moveToNext()) {
                int nameFieldColumnIndex = people.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
                String contact = people.getString(nameFieldColumnIndex);
                int numberFieldColumnIndex = people.getColumnIndex(ContactsContract.PhoneLookup.NUMBER);
                if (numberFieldColumnIndex >= 0) {
                    String number = people.getString(numberFieldColumnIndex);
                }
            }

    people.close();

String contact = people.getString(nameFieldColumnIndex); is working just fine, it stores all my contacts names, but int numberFieldColumnIndex = people.getColumnIndex(ContactsContract.PhoneLookup.NUMBER); is always -1. Could you tell me where's the problem? Thank you :)

miskohut
  • 957
  • 1
  • 14
  • 34

1 Answers1

0

Getting contact name correctly is just a co incidience.. as the value for ContactsContract.PhoneLookup.DISPLAY_NAME is "display_name" which is present in the result set.

And for ContactsContract.PhoneLookup.NUMBER is "number" which is not present in the result set you get after quering using ContactsContract.Contacts.CONTENT_URI

You can check which columns it will return by using following code first.

        if(people!=null){
        String[] colNames = people.getColumnNames();

        for(int i=0;i<colNames.length;i++)
            Log.i("",colNames[i]);
        }

Also do check the documentation for PhoneLookup by just moving your cursor on it (use intellij :) )

Here it is by the way:

*A table that represents the result of looking up a phone number, for example for caller ID. To perform a lookup you must append the number you want to find to CONTENT_FILTER_URI. This query is highly optimized. Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME,...

Columns PhoneLookup String NUMBER read-only Phone number. String TYPE read-only Phone number type. See CommonDataKinds.Phone. String LABEL read-only Custom label for the phone number. See CommonDataKinds.Phone. Columns from the Contacts table are also available through a join. Join with Contacts long _ID read- only Contact ID. String LOOKUP_KEY read-only See ContactsContract.Contacts String DISPLAY_NAME read-only See ContactsContract.Contacts long PHOTO_ID read-only See ContactsContract.Contacts. int IN_VISIBLE_GROUP read-only See ContactsContract.Contacts. int HAS_PHONE_NUMBER read-only See ContactsContract.Contacts. int TIMES_CONTACTED read-only See ContactsContract.Contacts. long LAST_TIME_CONTACTED read-only See ContactsContract.Contacts. int STARRED read-only See ContactsContract.Contacts. String CUSTOM_RINGTONE read-only See ContactsContract.Contacts. int SEND_TO_VOICEMAIL read-only See ContactsContract.Contacts.*

Farhan
  • 13,290
  • 2
  • 33
  • 59