从光标尝试getBlob时不支持的操作异常

时间:2013-05-23 07:10:01

标签: android listview simplecursoradapter android-cursoradapter android-cursor

以下是我的SimpleCursorAdapter扩展程序类,我试图在ListView中显示有关联系人的信息:

private class CustomContactsAdapter extends SimpleCursorAdapter {
        private int layout;
        private LayoutInflater inflater;

        public CustomContactsAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
            super(context, layout, c, from, to, 0);
            this.layout = layout;
            inflater = LayoutInflater.from(context);            
        }

        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            View v = inflater.inflate(layout, parent, false);
            return v;
        }

        @Override
        public void bindView(View v, Context context, Cursor cur) {
            MatrixCursor c = (MatrixCursor) cur;
            final String name = c.getString(c.getColumnIndex(COLUMN_NAME));
            final String org  = c.getString(c.getColumnIndex(COLUMN_ORG));
            final byte[] image = c.getBlob(c.getColumnIndex(COLUMN_PHOTO));

            ImageView iv = (ImageView) v.findViewById(R.id.contact_photo);

            if(image != null && image.length > 3) {
                iv.setImageBitmap(BitmapFactory.decodeByteArray(image, 0, image.length));
            }

            TextView tname = (TextView) v.findViewById(android.R.id.text1);
            tname.setText(name);

            TextView torg = (TextView) v.findViewById(android.R.id.text2);
            torg.setText(org);
        }
    }

但是当程序到达我想从光标获取blob数据的代码片段时,UnsupportedOperationException会抛出一条消息:

  

不支持getBlob

我想知道我做错了什么。另外,我将自己烤的MatrixCursor作为参数传递给适配器。

1 个答案:

答案 0 :(得分:1)

这是Android 1.6和Android 2.3中来自getBlob(int)的{​​{1}}的实现。

MatrixCurosr

这是Android ICS

public byte[] getBlob(int column) { throw new UnsupportedOperationException("getBlob is not supported"); } 实施
getBlob(int)

可能你想要继承 @Override public byte[] getBlob(int column) { Object value = get(column); return (byte[]) value; } 并以ICS方式实现getBlob