2

I've gotten this snippet from StackOverflow:

Cursor people = getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    try{
        while (people.moveToNext()) {
            int nameFieldColumnIndex = people
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            String contact = people.getString(nameFieldColumnIndex);
            int numberFieldColumnIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            String number =  people.getString(numberFieldColumnIndex);

            System.out.println(contact + "-" + number);
        }
    }catch(Exception e){
        System.out.println(e);
    }
    people.close();

When I try to read phoneNum column I get an error using String number = people.getString(numberFieldColumnIndex). Checking the column index I find that numberFiledColomnIndex = -1.

How can I get this snippet working?

Termininja
  • 6,620
  • 12
  • 48
  • 49
nijian81
  • 53
  • 9

1 Answers1

0

Per the documentation, getColumnIndex() returns -1 when the column doesn't exist.

Q: Are you passing the correct column name? Is the spelling correct?

FoggyDay
  • 11,962
  • 4
  • 34
  • 48
  • @user4769924 - Strong suggestion: step through the debugger. Check the values of your actual database column fields; check the spelling of the value you're passing to getColumnIndex(). The entire problem could be a typo. – FoggyDay Apr 11 '15 at 06:08