• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Filter contacts by phone number

QuickNick

Newbie
I try to make "type-to" filter in the my dialer. When I tap a digit, there must be a list (ListView) of supposed contacts (all of them have a number containing a part from my dialer TextView). For example, if I tap 555, there must be presented contacts with such numbers:
555890856
555789734
895550123

1st attempt:
Code:
String phoneNumberPart = //this is the part of phone number. Only part!
final Uri baseUri = ContactsContract.Contacts.CONTENT_FILTER_URI;
final Uri uri = Uri.withAppendedPath(baseUri, Uri.encode(phoneNumberPart));
final String[] projection = CONTACTS_PROJECTION; //_ID, DISPLAY_NAME, LOOKUP_KEY
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
2nd attempt:
Code:
String phoneNumberPart = //this is the part of phone number. Only part!
final Uri baseUri = ContactsContract.PhoneLookup.CONTENT_FILTER_URI;
final Uri uri = Uri.withAppendedPath(baseUri, phoneNumberPart);
final String[] projection = CONTACTS_PROJECTION; //_ID, DISPLAY_NAME, LOOKUP_KEY
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
In both attempts cursor count is always 0, despite I dial the phone number of real contact.

Is it possible to filter contacts by this criteria, using Android uris? Or there is no way but "get all contacts, iterate over them and check their numbers"?
 
Thanks, it works!

Also, I replace '-' in the columns. See:
Code:
        final StringBuilder selectionBuilder = new StringBuilder();
        selectionBuilder.append(ContactsContract.Data.MIMETYPE).append(" = ?").append(" AND REPLACE(");
        selectionBuilder.append(ContactsContract.CommonDataKinds.Phone.NUMBER).append(",'-','') LIKE ? ");
        final String[] selectionArgs = new String[] {//
        /*    */ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, //
                "%" + text + "%" //
        };

It would be very cool if I can make CONTACT_ID's unique/distinct.
I've tried add "DISTINCT" before column name in projection array, but I've got exception:
Code:
java.lang.IllegalArgumentException: Invalid column DISTINCT contact_id
Also, I tried to add "GROUP BY contact_id" in the selection but once again I've messed up.

Stackoverflow answers only questions about raw SQL query, not about ContentResolver.query();
 
Unfortunately ContentProviders are very limiting with regards to SQL support. So I'm not sure what to tell ya.

You could also try filtering your results at the adapter level with a custom Filter, and implementing Filterable. (The second link I have above talks briefly about filters).

But I dont know enough about the SQL stuff to offer any other tips on that, sorry :(
 
^ I think you mean that for another thread



Anyways, Nick, did you ever get this to work? I am going to be working on similar stuff, soon so I am curious how well it worked.
 
Back
Top Bottom