我想用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"开头的电话的联系方式。所以它将字母"转换"为数字。我不想这样。我该怎么办?
下面是按姓名搜索联系人的代码片段。也许你会发现少了什么:
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;
}