Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Find a contact by display name

I'm trying to find a contact by display name. The goal is to open this contact and add more data to it (specifically more phone numbers), but I'm struggling to even find the contact I want to update.

This is the code I'm using:

    public static String findContact(Context context) {

    ContentResolver contentResolver = context.getContentResolver();
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI;
    String[] projection = new String[] { PhoneLookup._ID };
    String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " = ?";
    String[] selectionArguments = { "John Johnson" };
    Cursor cursor = contentResolver.query(uri, projection, selection, selectionArguments, null);

    if (cursor != null) {
        while (cursor.moveToNext()) {
            return cursor.getString(0);
        }
    }
    return "John Johnson not found";
}

I do have a contact called "John Johnson", but the method always returns "not found". I also tried searching for a contact with just one name, so that makes no difference.

I suspect that it's something wrong with the uri, selection or selection arguments, because I couldn't find any example online of searching for contacts with a given display name, and it seems display name is a special kind of information, different from for example a phone number.

Any ideas how I can achieve to find John Johnson?


UPDATE: I found out how to find a contact by display name:

        ContentResolver contentResolver = context.getContentResolver();
    Uri uri = Data.CONTENT_URI;
    String[] projection = new String[] { PhoneLookup._ID };
    String selection = StructuredName.DISPLAY_NAME + " = ?";
    String[] selectionArguments = { "John Johnson" };
    Cursor cursor = contentResolver.query(uri, projection, selection, selectionArguments, null);

    if (cursor != null) {
        while (cursor.moveToNext()) {
            return cursor.getString(0);
        }
    }
    return "John Johnson not found";

This code returns the contact id of the first contact with display name "John Johnson". In my original code I had the wrong uri and the wrong selection in my query.

like image 290
Bjarte Aune Olsen Avatar asked Mar 08 '12 21:03

Bjarte Aune Olsen


People also ask

How can I find a contact by name?

From the Contacts screen, press the Menu button, and then tap Search. Start typing a person's name in the search box.

How do I select and display my phone number from a contact list on Android?

Open your Contacts app and tap the Options button (three dots), and select Contacts Manager. On the next screen, tap on Contacts to display from the menu. Next, if you only want contacts with a phone number, tap on Phone.


1 Answers

I thinks the issue may caused by the projection you set. Projection is used to tell android which column of data you want to query then you only give the id column so the display name won't return. Try to remove the projection to see whether it works.

-- Cursor cursor = contentResolver.query(uri, projection, selection, selectionArguments, null);
++ Cursor cursor = contentResolver.query(uri, null, selection, selectionArguments, null);

like image 104
Frank Cheng Avatar answered Sep 22 '22 13:09

Frank Cheng