How to search for contacts in android with partial phone number

How to search for contacts in android with partial phone number



I am trying to do fuzzy search of contacts in my android application with partial phone number, with below code. But my search function always ends in no result.



For example,



I have contact with phone number 1234567890 with name as "example".



fuzzySearch("4567"); should have return with contact named "example".


fuzzySearch("4567");



Can some please point me where am I wrong? I have checked here. But ended in run-time sql query error.


public ArrayList<Contact> fuzzySearch(String match)
private static final String SELECTION = Phone.NUMBER + " LIKE ? COLLATE NOCASE";

Uri uri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(match));

String projectionFields = new StringContactsContract.Contacts._ID, Phone.NUMBER;

CursorLoader cursorLoader = null;

ArrayList<Contact> listContacts = new ArrayList<>();

if(match.trim().length() == 0)
return listContacts;
else
cursorLoader = new CursorLoader(context, uri, projectionFields, SELECTION, new String"%"+match+"%", Phone.NUMBER);


Cursor c = cursorLoader.loadInBackground();

final Map<String, Contact> contactsMap = new HashMap<>(c.getCount());

if (c.moveToFirst())
int idIndex = c.getColumnIndex(ContactsContract.Contacts._ID);
int nameIndex = c.getColumnIndex(Phone.NUMBER);

do
String contactId = c.getString(idIndex);
String contactDisplayName = c.getString(nameIndex);
Contact contact = new Contact(contactId, contactDisplayName);
contactsMap.put(contactId, contact);
listContacts.add(contact);
while (c.moveToNext());


c.close();
return listContacts;




1 Answer
1



use PhoneLookup instead of phone.
because you already encode the search key into the URL, you don't need SELECTION, remove it.


Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(match));
Cursor cur = resolver.query(uri, new StringPhoneLookup.DISPLAY_NAME,PhoneLookup.NUMBER, PhoneLookup.CONTACT_ID, null, null, null);






I tried this, but did not work :(

– Sandy
Sep 10 '18 at 5:42



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)