Matthew Hoggan
Matthew Hoggan

Reputation: 7604

Android Getting a Contacts Name Keeps Returing a -1

I have an Intent that is sent to the Contacts Activity then the Main Activity gets the results in this Callback. The problem is that column is always -1. This being said how do I get the contacts name?

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PICK_CONTACT_REQUEST) {
        if (resultCode == RESULT_OK) {
            Uri contactUri = data.getData();
            String[] projection = {Phone.NUMBER};
            Cursor cursor = getContentResolver().query(contactUri, 
                        projection, null, null, null);
            if (cursor.moveToFirst()) {
            int column = cursor.getColumnIndex(
                ContactsContract.PhoneLookup.DISPLAY_NAME);
            if (column >= 0) {
                String name = cursor.getString(column);
                addString(name);
            } else {
                addString("Could Not Get User Name");
                }
            }
        }
    }

}

Upvotes: 0

Views: 33

Answers (1)

Marcio Covre
Marcio Covre

Reputation: 4556

Your projection is String[] projection = {Phone.NUMBER}; so the cursor will have only this column name, you need to use String[] projection = {Phone.NUMBER, ContactsContract.PhoneLookup.DISPLAY_NAME}; to have access to Display_Name.

Also make sure that the ContentProvider returns the information you want.

Upvotes: 1

Related Questions