我想将所有联系人的姓名和电话号码加载到 AutoCompleteTextView 的适配器中。我怎样才能做到这一点?例如,当我键入“G”时,它会在其下拉列表中显示“Good, <111111>”、“Good, <222222>”。
使用 api 演示,我只能将 DISPLAY_NAMEs 放在结果光标中。我不知道如何将名称和数字组合到一个光标中。谢谢!
来自 api 演示的代码:
ContentResolver content = getContentResolver();
Cursor cursor = content.query(ContactsContract.Contacts.CONTENT_URI,
PEOPLE_PROJECTION, null, null, null);
ContactListAdapter adapter = new ContactListAdapter(this, cursor);
mAutoCompleteTextView.setAdapter(adapter);
private static class ContactListAdapter extends CursorAdapter implements Filterable {
private ContentResolver mCR;
public ContactListAdapter(Context context, Cursor c) {
super(context, c);
mCR = context.getContentResolver();
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
final TextView view = (TextView) inflater.inflate(
android.R.layout.simple_dropdown_item_1line, parent, false);
view.setText(cursor.getString(1));
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
((TextView) view).setText(cursor.getString(1));
}
@Override
public String convertToString(Cursor cursor) {
return cursor.getString(1);
}
@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(ContactsContract.Contacts.DISPLAY_NAME);
buffer.append(") GLOB ?");
args = new String[] { constraint.toString().toUpperCase() + "*" };
}
return mCR.query(ContactsContract.Contacts.CONTENT_URI, PEOPLE_PROJECTION,
buffer == null ? null : buffer.toString(), args, null);
}
}
private static final String[] PEOPLE_PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER
};