1

I have a function that takes a number and seeks for it into the phone contacts, but after that it returns the contact name.

public String[] getContactDisplayNameByNumber(String number) {
        Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
        String name = "?";
        String[] nameStr = {};

        ContentResolver contentResolver = getContentResolver();
        Cursor contactLookup = contentResolver.query(uri, new String[] {BaseColumns._ID,ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);

        try {
            while (contactLookup != null && contactLookup.getCount() != 0 && !contactLookup.isLast()) {
                contactLookup.moveToNext();
                nameStr = insertElement(nameStr,contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));
                //String contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));
            }
        } finally {
            if (contactLookup != null) {
                contactLookup.close();
            }
        }

        return nameStr;
    }

What do I got to modify to make it search for a part of a number? ex if the number is:0729587347, I want to search for 0729. If there are many contact starting with that number, I want them all(It is something like MYSQL %variable% search ...)! What's to do ? Thanks !

Kara
  • 6,115
  • 16
  • 50
  • 57

1 Answers1

0

The ContentResolver.query(...) can take a WHERE clause parameter.

Specifically, use something like:

Cursor c = getContentResolver().query(Data.CONTENT_URI,
      new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
      Phone.NUMBER + " like ?",
      new String[] {"%"+String.valueOf(userInput) +"%"}, null);

For more examples, see the ContactsContract.Data javadoc.

Also, this is similar to a previous question.

Community
  • 1
  • 1
SoftWyer
  • 1,986
  • 28
  • 33