在安卓系统中智能搜索联系人

5

在Android开发者网站的“检索联系人列表”教程中,我成功地实现了联系人搜索功能。以下是我的代码:

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());
        }
    }

问题来了,这段代码运行得非常好,但我需要实现一种智能搜索。我希望 26268 能够与 “Amanu” 以及 “094 526 2684” 这些匹配。 我相信这被称为 T9 字典。

虽然我尝试查看其他项目寻找线索,但我没有找到任何有用的信息。如果您有任何建议,将不胜感激!

3个回答

3
T9搜索可以使用trie数据结构来实现。您可以在这里看到一个例子 - Trie字典。 实现类似的内容后,您将能够将搜索输入转换为可能的T9解码变体,并比较它是否与名称匹配。

你已经实现了这个吗?我不知道如何在我的项目中使用,请帮忙。 - Sagar

1
将所有联系人转储到 HashSet 中。
Set<String> contacts = new HashSet<String>();

然后搜索:

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);

搜索方法 (来自@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();
        }
    }
}

0

联系人的ContentProvider不支持此功能。因此,我所做的是将所有联系人都转储到一个List中,然后使用RegEx来匹配名称。

public static String[] values = new String[]{" 0", "1", "ABC2", "DEF3", "GHI4", "JKL5", "MNO6", "PQRS7", "TUV8", "WXYZ9"};

/**
 * Get the possible pattern
 * You'll get something like ["2ABC","4GHI"] for input "14"
 */
public static List<String> possibleValues(String in) {

    if (in.length() >= 1) {
        List<String> p = possibleValues(in.substring(1));
        String s = "" + in.charAt(0);
        if (s.matches("[0-9]")) {
            int n = Integer.parseInt(s);

            p.add(0, values[n]);
        } else {
            // It is a character, use it as it is
            p.add(s);
        }

        return p;
    }
    return new ArrayList<>();
}

然后编译模式。我使用了(?i)使其不区分大小写。

List<String> values = Utils.possibleValues(query);
StringBuilder sb = new StringBuilder();
for (String value : values) {
    sb.append("[");
    sb.append(value);
    sb.append("]");
    if (values.get(values.size() - 1) != value) {
    sb.append("\\s*");
    }
}

Log.e("Utils", "Pattern = " + sb.toString());

Pattern queryPattern = Pattern.compile("(?i)(" + sb.toString() + ")");

做完这个,你就知道该怎么做了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接