Get all informations of specific contact by _ID
See this source:
private void populateContactList() {
// Build adapter with contact entries
Cursor cursor = getContacts();
/*
String[] fields = new String[] {ContactsContract.Data.DISPLAY_NAME};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
fields, new int[] {R.id.contactEntryText});
mContactList.setAdapter(adapter);*/
while(cursor.moveToNext()){
int id = cursor.getInt(0);
String nome = cursor.getString(1);
Log.i("ContactsTest", id + " - "+ nome);
}
}
/**
* Obtains the contact list for the currently selected account.
*
* @return A cursor for for accessing the contact list.
*/
private Cursor getContacts()
{
// Run query
// se tem número de telefone
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
};
//trazer só os contatos que possuem开发者_运维技巧 ao menos um número.
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = " + "0";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
how get all informations of one user by _ID inside while loop?
Can you help me?
Thanks
Mateus Dias
not sure if it helps but here is a snipped i use to get all contact data
android get all contacts // // Find contact based on name. // ContentResolver cr = getContentResolver(); Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, "DISPLAY_NAME = '" + NAME + "'", null, null); if (cursor.moveToFirst()) { String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); // // Get all phone numbers. // Cursor phones = cr.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = " + contactId, null, null); while (phones.moveToNext()) { String number = phones.getString(phones.getColumnIndex(Phone.NUMBER)); int type = phones.getInt(phones.getColumnIndex(Phone.TYPE)); switch (type) { case Phone.TYPE_HOME: // do something with the Home number here... break; case Phone.TYPE_MOBILE: // do something with the Mobile number here... break; case Phone.TYPE_WORK: // do something with the Work number here... break; } } phones.close(); // // Get all email addresses. // Cursor emails = cr.query(Email.CONTENT_URI, null, Email.CONTACT_ID + " = " + contactId, null, null); while (emails.moveToNext()) { String email = emails.getString(emails.getColumnIndex(Email.DATA)); int type = emails.getInt(emails.getColumnIndex(Phone.TYPE)); switch (type) { case Email.TYPE_HOME: // do something with the Home email here... break; case Email.TYPE_WORK: // do something with the Work email here... break; } } emails.close(); } cursor.close(); Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, personId); Uri phonesUri = Uri.withAppendedPath(personUri, People.Phones.CONTENT_DIRECTORY); String[] proj = new String[] {Phones._ID, Phones.TYPE, Phones.NUMBER, Phones.LABEL} Cursor cursor = contentResolver.query(phonesUri, proj, null, null, null);
精彩评论