Android Contact Picker Checklist
I'm trying to create a "select-multiple" contact list where I can allow a user to check more than one contact. What I'm looking for is effectively the same thing as the native activity that 开发者_Go百科appears when composing a message to multiple contacts. Thanks!
At first you need to get a list of contacts and then display it on ListView element. For example I'm using following code to display all users contacts on a ListView:
// Run query on all contacts id
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
String selection = null;//ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '" + ("1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
ContentResolver contectResolver = getContentResolver();
Cursor cursor = contectResolver.query(uri, projection, selection, selectionArgs,
sortOrder);
//Create buffer
final ArrayList<ContactData> bufferContacts = new ArrayList<ContactData>();
//Load contacts one by one
if(cursor.moveToFirst()) {
while(!cursor.isAfterLast()) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String[] emailProj = new String[]{Email.DATA};
Cursor cursorEmail = contectResolver.query(Email.CONTENT_URI, emailProj,Email.CONTACT_ID + " = ? ", new String[] { id }, null);
String[] phoneProj = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
Cursor cursorPhone = contectResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, phoneProj,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
String firstName = "";
String lastName = "";
String email = "";
String displayname = "";
String phoneNmb = "";
if(cursorPhone.moveToFirst()) {
///displayname = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNmb = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
cursorPhone.close();
if(cursorEmail.moveToFirst()) {
email = cursorEmail.getString(cursorEmail.getColumnIndex(Email.DATA));
}
cursorEmail.close();
displayname = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//Divide display name to first and last
String[] names = displayname.split("\\s+");
firstName = displayname;
if(names.length >= 1) {
firstName = names[0];
}
if(names.length >= 2) {
firstName = names[1];
}
final ContactData contactData = new ContactData(id, firstName, lastName, phoneNmb, email, allChecked);
bufferContacts.add(contactData);
//Set list view initialy
runOnUiThread(new Runnable() {
public void run() {
if(contactsAdapter == null) {
ArrayList<ContactData> contacts = new ArrayList<ContactData>();
contactsAdapter = new ContactAdapter(ContactPickerActivity.this, contacts);
lvContacts.setAdapter(contactsAdapter);
}
if(bufferContacts.size() >= BUFFER_INTERVAL) {
addBuffer(bufferContacts);
}
}
});
cursor.moveToNext();
}
}
cursor.close();
runOnUiThread(new Runnable() {
public void run() {
addBuffer(bufferContacts);
}
});
If you don't want to bother yourself with making everything out of scratch then feel free to use my ready library for selecting multiple contacts:
https://github.com/kgadzinowski/Android-Multiple-Contacts-Picker-Library
精彩评论