我正在使用以下代码在设备上构建要在ListActivity
上显示的电子邮件地址列表。目前我可以检索确定的电子邮件地址,但它们会以null (email@address.com)
而不是name (email@address.com)
的形式出现,这并不理想。我正在使用的代码也应该检索名称:
Cursor c = getContentResolver().query(Email.CONTENT_URI,
new String[]{Email.CONTACT_ID, Email.DISPLAY_NAME, Email.DATA},
null, null, null);
addresses = new String[c.getCount()];
try{
c.moveToFirst();
for(int i = 0;i<c.getCount();i++){
addresses[i] = c.getString(1) + " (" + c.getString(2) + ")";
c.moveToNext();
}
} finally {
c.close();
}
有谁知道我做错了什么?
答案 0 :(得分:6)
我已经使用了这个(绝对适用于不可思议的Android 2.2; 2.3上没有承诺)。
String nameAndEmailOrder = "lower(" + ContactsContract.Data.DISPLAY_NAME + ") ASC";
String[] nameAndEmailProjection = new String[] {
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA }
Cursor emails = activity.managedQuery(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
nameAndEmailProjection, null, null, nameAndEmailOrder);
emails.moveToFirst();
int nameColumn = emails.getColumnIndex(
ContactsContract.Data.DISPLAY_NAME);
int emailColumn = emails.getColumnIndex(
ContactsContract.CommonDataKinds.Email.DATA);
do {
Log.d("test", String.format("%s (%s)", emails.getString(nameColumn), emails.getString(emailColumn)));
} while (emails.moveToNext());
答案 1 :(得分:0)
尝试此查询:
Cursor c = managedQuery(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Email.DATA},
null, null, null);
答案 2 :(得分:0)
尝试此查询:
Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[]{Data.CONTACT_ID, Data.DISPLAY_NAME, Email.ADDRESS},
Data.MIMETYPE + "=?", new String[] {Email.CONTENT_TYPE}, null);
addresses = new String[c.getCount()];
try{
c.moveToFirst();
for(int i = 0;i < c.getCount();i++){
addresses[i] = c.getString(1) + " (" + c.getString(2) + ")";
c.moveToNext();
}
} finally {
c.close();
}
注意:Email.ADDRESS将转换为Data.DATA1。