Android 通过_ID获取特定联系人的所有信息
见此来源:Android 通过_ID获取特定联系人的所有信息,android,Android,见此来源: private void populateContactList() { // Build adapter with contact entries Cursor cursor = getContacts(); /* String[] fields = new String[] {ContactsContract.Data.DISPLAY_NAME}; SimpleCursorAdapter adapter = new SimpleCurso
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);
}
如何通过while循环中的_ID获取一个用户的所有信息
你能帮我吗
谢谢
Mateus Dias不确定这是否有帮助,但这里有一个我用来获取所有联系数据的剪报 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); android获取所有联系人 // //根据姓名查找联系人。 // ContentResolver cr=getContentResolver(); Cursor Cursor=cr.query(ContactsContract.Contacts.CONTENT\u URI,null, “显示名称='”+NAME+“'”,空,空); if(cursor.moveToFirst()){ 字符串联系人ID= cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); // //获取所有电话号码。 // 游标phones=cr.query(Phone.CONTENT\u URI,null, Phone.CONTACT_ID+“=”+contactId,null,null); while(phones.moveToNext()){ 字符串编号=phones.getString(phones.getColumnIndex(Phone.number)); int type=phones.getInt(phones.getColumnIndex(Phone.type)); 开关(类型){ case Phone.TYPE_HOME: //用这里的家庭号码做点什么。。。 打破 手机壳。手机类型: //用这里的手机号码做点什么。。。 打破 case Phone.TYPE_工作: //用这里的工作号码做点什么。。。 打破 } } 电话。关闭(); // //获取所有电子邮件地址。 // 游标emails=cr.query(Email.CONTENT\u URI,null, Email.CONTACT_ID+“=”+contactId,null,null); while(emails.moveToNext()){ 字符串email=emails.getString(emails.getColumnIndex(email.DATA)); int type=emails.getInt(emails.getColumnIndex(Phone.type)); 开关(类型){ case Email.TYPE_HOME: //在这里用家庭电子邮件做点什么。。。 打破 案例Email.TYPE_工作: //在这里用工作邮件做点什么。。。 打破 } } 电子邮件。关闭(); } cursor.close(); uripersonuri=ContentUris.withAppendedId(People.CONTENT\u Uri,personId); Uri phonesUri=Uri.withAppendedPath(personUri,People.Phones.CONTENT\u目录); String[]proj=新字符串[]{Phones.\u ID,Phones.TYPE,Phones.NUMBER,Phones.LABEL} Cursor Cursor=contentResolver.query(phonesUri、proj、null、null、null);