3

I want to implement a contact search with simplecursoradapter. And it should behave like standard android contact search. The problem is I can't write filter right. 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 in filter a letter, 'm' for example, this filter gives me contacts which phones starts with '5'. So it "cast" letters to numbers. And I don't want this. What should I do?

Ov3r1oad
  • 1,057
  • 1
  • 12
  • 25

1 Answers1

4

Here is my code snippet for searching contacts by name. Maybe you'll find something is missing:

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;
    }
Chris
  • 3,329
  • 1
  • 30
  • 44
  • Well, it's kinda valid code for my case. Though this selection finds parts of "name" in DISPLAY_NAME. And I need that only contacts with DISPLAY_NAME which starts with "name" are found. – Ov3r1oad Aug 14 '14 at 12:40
  • Uh, oh. SQL injection. – HelloWorld Jun 28 '16 at 19:32