Pedro Tentugal
Pedro Tentugal

Reputation: 94

Android - get contact where phone number LIKE something being typed

I've been trying to get a contact by it's number, the same way Android has implemented when your dialing a number on the stock dialer. If you're familiar, you know that when you are dialing it starts to show matches that contain those numbers (913284912 would match when dialing 3412 for example). (not sure if this way of search was only implemented on 5.x+)

I got this to work but only when the number is exactly equal to the contact one with the code below:

private Cursor getContacts(String number) {
    // Run query
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number.trim()));
    String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME,
            ContactsContract.PhoneLookup.PHOTO_THUMBNAIL_URI, ContactsContract.PhoneLookup._ID};
    return getContext().getContentResolver().query(
            uri,
            projection,
            null,
            null,
            null
    );
}

I'm having a bit difficulty on finding solutions for this, as everyone uses this to get a contact. But as I don't want an exact match, this won't do for me.

Thanks in advance.

Upvotes: 0

Views: 239

Answers (1)

Gavriel
Gavriel

Reputation: 19237

You can use an Adapter with a Filter. Some examples can be found: Filtering ListView with custom (object) adapter or even better Custom Listview Adapter with filter Android

Upvotes: 1

Related Questions