Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smart searching contacts in android

Following This Retrieving a List of Contacts Tutorial in the android developers site, I managed to implement contacts search functionality. Here is my code so far

private void retrieveContactRecord(String phoneNo) {
        try {
            Log.e("Info", "Input: " + phoneNo);
            Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                    Uri.encode(phoneNo));
            String[] projection = new String[]{ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME};


            String sortOrder = ContactsContract.PhoneLookup.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
            ContentResolver cr = getContentResolver();
            if (cr != null) {
                Cursor resultCur = cr.query(uri, projection, null, null, sortOrder);
                if (resultCur != null) {
                    while (resultCur.moveToNext()) {
                        String contactId = resultCur.getString(resultCur.getColumnIndex(ContactsContract.PhoneLookup._ID));
                        String contactName = resultCur.getString(resultCur.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
                        Log.e("Info", "Contact Id : " + contactId);
                        Log.e("Info", "Contact Display Name : " + contactName);
                        break;
                    }
                    resultCur.close();
                }
            }
        } catch (Exception sfg) {
            Log.e("Error", "Error in loadContactRecord : " + sfg.toString());
        }
    }

Here is the catch, this code works pretty great, but I need to implement a smart search here. I want 26268 to match Amanu as well as 094 526 2684. I believe it is called T9 dictionary.

I tried looking at other projects for clue, but I couldn't find anything. Any pointers would be appreciated!

like image 358
Amanuel Nega Avatar asked Sep 23 '22 03:09

Amanuel Nega


People also ask

Why my contacts are not showing while searching?

Sync all your contacts with Google account: But just in case the contacts have not been synced yet, try syncing your contacts manually. And it might help in contacts to show up. Follow the guide below. The first step, Go to the Settings app.


2 Answers

T9 search can be implemented using trie data structure. You can see an example here - Trie dict. After implementing something similar you will be able to convert your search input into its possible T9 decoded variant and compare if it matches with name.

like image 112
Yaroslav Avatar answered Nov 26 '22 23:11

Yaroslav


Dump all contacts to a HashSet

Set<String> contacts = new HashSet<String>();

Then search:

List<List<String>> results = new ArrayList<List<String>>();
// start the search, pass empty stack to represent words found so far
search(input, dictionary, new Stack<String>(), results);

Search method (from @WhiteFang34)

public static void search(String input, Set<String> contacts,
    Stack<String> words, List<List<String>> results) {

    for (int i = 0; i < input.length(); i++) {
        // take the first i characters of the input and see if it is a word
        String substring = input.substring(0, i + 1);

        if (contacts.contains(substring)) {
            // the beginning of the input matches a word, store on stack
            words.push(substring);

            if (i == input.length() - 1) {
                // there's no input left, copy the words stack to results
                results.add(new ArrayList<String>(words));
            } else {
                // there's more input left, search the remaining part
                search(input.substring(i + 1), contacts, words, results);
            }

            // pop the matched word back off so we can move onto the next i
            words.pop();
        }
    }
}
like image 22
Ozgur Avatar answered Nov 26 '22 22:11

Ozgur