开发者

Problem with Email ContentResolver

I'm using the following code to build a list of email addresses on the device to display on a ListActivity. Currently I can retrieve the email addresses ok, but they're coming up in the form: null (email@address.com) instead of name (email@address.com) which is not ideal. The code I'm using should be retrieving t开发者_运维百科he names as well:

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();
}

Does anyone know what I'm doing wrong?


I've used this (definitely works on Android 2.2 on the Incredible; no promises on 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());


Try this query:

Cursor c = managedQuery(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
            new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Email.DATA},
            null, null, null);


Try this query :

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();
}

Note: Email.ADDRESS will be converted to Data.DATA1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜