【问题标题】:Custom column names for cursor from a content provider来自内容提供者的光标自定义列名
【发布时间】:2012-07-12 22:03:17
【问题描述】:

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

我试试下面的

   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

【问题讨论】:

    标签: android android-contentprovider


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多