I want to load all of my contacts' names and phone numbers into an AutoCompleteTextView's adapter. How can I achieve that? for example, when i type "G", it will show "Good, <111111>", "Good, <222222>" in its drop-down list.
with the api demo, i can only put the DISPLAY_NAMEs in the result cursor. I don't know how to combine both names and numbers into one cursor. thanks!
codes from the api demo:
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 b开发者_如何学Gouffer = 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
};
The numbers should already be part of the cursor. Retrieve them like so.
final long phoneNumber;// = c.getColumnIndex(People.NUMBER_KEY);
if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0){
id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
int nameColumn = cursor.getColumnIndex(People.NAME);
int numberKeyColumn = cursor.getColumnIndex(People.NUMBER_KEY);
phoneNumber = this.getPhoneNumbers(id); }
Then the getPhoneNumbers method, in the same class, unforunately has to iterate again. Not sure if there's a better way to do this by binding to a cursor (in the new api), but it works.
public Long getPhoneNumbers(String id) {
Long number = new Long(0);
ContentResolver cr = this.context.getContentResolver();
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
pCur.moveToFirst();
if (pCur.getCount() > 0){
String num = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
num = num.replaceAll("-", "");
number = Long.parseLong(num);
}
pCur.close();
return number;
}
精彩评论