0

I am trying to find out if the caller id is in the contacts list. So the code I am using for that:

    ContentResolver resolver = this.service.getContentResolver();
    String number = PhoneLookup.NUMBER;
    String[] projections = { number };
    String selectionClause = number + " = ?";
    String[] selectionClauseArgs = { callerId };
    Cursor people = resolver.query(
            ContactsContract.Contacts.CONTENT_URI, projections,
            selectionClause, selectionClauseArgs, null);
    return people.getCount() > 0 ? true : false;

I am giving the filtering functionality to the ContentResolver itself instead of fetching everything, iterating over them and checking one by one.

But I receive the following error for something reason (There are many codes but they are working just fine, since they are just subscribing to the telephony events in order to receive caller id when some one is calling!")

08-28 19:20:05.903: E/AndroidRuntime(737): java.lang.IllegalArgumentException: Invalid column number

I don't what is invalid. I am using right column name which is PhoneLookup.NUMBER constant. A similar question was asked here before: How to read contacts on Android 2.0 which I followed.

Community
  • 1
  • 1
Tarik
  • 79,711
  • 83
  • 236
  • 349

1 Answers1

0

There's no phone numbers in this table. You should query it from another place. Here's fast scratch (I belive there's better way to do it):

boolean hasPhoneNumber = (people.getInt(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0);
        if (hasPhoneNumber) {
            String id = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
            String num = getNumber(id);
        }


public String getNumber(String id) {
    String number = null;
    String name = null;
    Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
    if (pCur.moveToFirst()) {
        number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        name = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
    }
    pCur.close();
    return number;
}
Fedor Kazakov
  • 601
  • 3
  • 12
  • I believe `people` is a `Cursor` object but with which arguments it was filled? – Tarik Aug 29 '12 at 14:09
  • And also I remember this code somewhere. It would have been better if you had put the link of the source page so I see the rest of the code. – Tarik Aug 29 '12 at 14:20
  • 'people' queried with nulls, means its every contacts with full fields. Code is from github but I've lost link to it, sorry :) – Fedor Kazakov Aug 30 '12 at 07:08