我想用simplecursoradapter实现联系人搜索。它应该像标准的Android联系人搜索一样。问题是我无法正确编写过滤器。现在我有这样的事情:
private FilterQueryProvider filterQueryProvider = new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence Constraint) {
ContentResolver contentResolver = getActivity().getContentResolver();
Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI,Uri.encode(Constraint.toString()));
String[] projection = { BaseColumns._ID, Phone.PHOTO_URI, Phone.DISPLAY_NAME, Phone.NUMBER, Phone.TYPE };
return contentResolver.query(
uri,
projection,
null,
null,
"upper(" + Phone.DISPLAY_NAME + ") ASC");
}
};
它有效,但有一件事。例如,当我输入一个字母'm'时,这个过滤器会给我联系,哪些电话以'5'开头。所以它“拼”字母到数字。我不想要这个。我该怎么办?
答案 0 :(得分:4)
以下是我按名称搜索联系人的代码段。也许你会发现缺少的东西:
public String getPhoneNumber(String name, Context context) {
String ret = null;
String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" like'%" + name +"%'";
String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor c = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, selection, null, null);
if (c.moveToFirst()) {
ret = c.getString(0);
}
c.close();
if(ret==null)
ret = "Unsaved";
return ret;
}