1

是否可以使用自定义列名而不是内容提供者提供的那些来获取光标?

我尝试以下

   private final String[] PROJ_CONTACT = {
                    ContactsContract.CommonDataKinds.Email.DISPLAY_NAME_PRIMARY + " AS " + Golfer.COLUMN_NAME,
                    ContactsContract.CommonDataKinds.Email.ADDRESS + " AS " + Golfer.COLUMN_EMAIL
            };

ContentResolver resolver = getActivity().getContentResolver();
Cursor cursor = resolver.query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    PROJ_CONTACT,
                    SELECTION_BY_ID,
                    new String[] {Long.toString(id)},
                    null);

但我得到了例外07-12 17:02:01.733: E/AndroidRuntime(7569): java.lang.IllegalArgumentException: Invalid column display_name AS name

4

1 回答 1

0

可能的解决方案是创建一个游标包装器,它重新映射列名并使用它而不是内容提供程序返回的游标,但也许有更好的解决方案?

private class ContactCursor extends CursorWrapper {
    public ContactCursor(Cursor cursor) {
        super(cursor);
    }

    @Override
    public int getColumnIndex(String columnName) {
        if (Golfer.COLUMN_NAME.equalsIgnoreCase(columnName)){
            columnName = ContactsContract.CommonDataKinds.Email.DISPLAY_NAME_PRIMARY;
        }

        if (Golfer.COLUMN_EMAIL.equalsIgnoreCase(columnName)){
            columnName = ContactsContract.CommonDataKinds.Email.ADDRESS;
        }               

        return super.getColumnIndex(columnName);
    }
}
于 2012-07-12T22:15:22.207 回答