1

I would like to implement T9 search like android native dialer or TrueCaller dialer. What I have achieved till now is:-

Convert keystrokes to string patterns like if I press 2 on dialer it would gives me - "abc" "ac" "ca" and so on

Now I made a query

String selection = Utility.getSelectionLikeWithIn(mmemonics);

Cursor query = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, selection, null, null);'

public static String getSelectionLikeWithIn(List mmemonics) {

    String selection = "";
    for (int i = 0; i < mmemonics.size(); i++) {
        selection = selection + " display_name LIKE '";
        selection = selection + mmemonics.get(i);

        if (i == mmemonics.size() - 1){
            selection = selection + "%'";


        }else {
            selection = selection + "%' OR";
        }
    }
    return selection;
}'

above method convert Arraylist of string to where clause

where display_name Like 'ab%' OR display_name LIKE 'abc%' and so on......

well this give me the inefficient result and app crash due to complex query.....

Please suggest me how can I perform a query to match like my keystroke or os their any other way in ContactsProvider to perform such smart search

Vikas Rohilla
  • 31
  • 1
  • 2
  • 4

1 Answers1

6

If you're going to refresh the list on each digit press this technique won't work because it'll be soooo slow.

The better option would be to read all contact names from the Contacts DB, and store them in a simple ArrayList<String> or similar collection, preferably while reading the names convert them toLowerCase() to make search easier in the next step.

Next, for each digit pressed, build a regular expression pattern from the entered digits, for example "234" would convert to the pattern .*[abc][def][ghi].*

After building the pattern, iterate through the names list and return names that match the pattern.

marmor
  • 27,641
  • 11
  • 107
  • 150