Find contacts from Android
I want to implement contact search using simplecursoradapter. And it should behave like a standard android contact search. The problem is that I cannot write the filter correctly. Now I have something like this:
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");
}
};
And it works, but there is a thing. When I put a letter, for example "m" in the filter, this filter gives me contacts whose phones start with "5". So these are "cast" letters into numbers. And I don't want that. What should I do?
+3
source to share
1 answer
Here is my code snippet for searching contacts by name. You may find that you are missing something:
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;
}
+4
source to share