I have a field in an Android app that I would like to autocomplete from the user's contacts. I can get it to autocomplete based on partial entry of a particular name from the contacts, but I would like for it to autocomplete based on entering either the name or the email address of the contact. Here's the relevant code:
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
if (getFilterQueryProvider() != null) {
return getFilterQueryProvider().runQuery(constraint);
}
StringBuilder buffer = null;
String[] args = null;
if (constraint != null) {
buffer = new StringBuilder();
buffer.append("(UPPER(");
buffer.append(Email.DATA1);
buffer.append(") GLOB ?");
System.out.println(buffer.toString());
args = new String[] { constraint.toString().toUpperCase() + "*" };
}
String selection = buffer == null ? null : buffer.toString();
return mContent.query(
uri,
projection,
selection,
args,
sortOrder);
}
If I replace:
buffer.append(Email.DATA1);
with
buffer.append(ContactsContract.Contacts.DISPLAY_NAME);
The autocomplete will initiate based on the names in the contact list. How do I format a well-formed query to initiate the autocomplete for either the name or the email?