ContentProvider
for contacts does not support it. So what I did was to reset all the contacts in the List
, and then use RegEx
to match the name.
public static String[] values = new String[]{" 0", "1", "ABC2", "DEF3", "GHI4", "JKL5", "MNO6", "PQRS7", "TUV8", "WXYZ9"}; 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 {
.... Then compile the template. I used (?i)
to make case insensitive
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() + ")");
You will learn what to do after that.
source share