0

Here is my SimpleCursorAdapter extension class which I use trying to display information about contacts in a 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);
        }
    }

But when the program reaches the code snippet where I want to get blob data from cursor an UnsupportedOperationException is thrown there with message:

getBlob is not supported

I want to know what am I doing wrong. Also, I pass a MatrixCursor baked by myself as a parameter to the adapter.

Alex Bonel
  • 1,344
  • 1
  • 9
  • 22

1 Answers1

1

That's the implemetaion of getBlob(int) from MatrixCurosr in Android 1.6 and Android 2.3.

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

That's the getBlob(int) implementation for Android ICS

 @Override
 public byte[] getBlob(int column) {
        Object value = get(column);
        return (byte[]) value;
 }

probably you want to subclass MatrixCursor and implment the getBlob in the ICS way

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Thanks for mention about approaches in different versions, will know about it. But as I said I use Android 4.2.2 and run this app on 2.3 emulator. – Alex Bonel May 23 '13 at 07:34
  • Where do you run it is what matter (does make sense my sentence) – Blackbelt May 23 '13 at 07:35
  • Oh, I saw it to late that you've added Android 2.3 to your answer looks like I really have to override `MatrixCursor` logic – Alex Bonel May 23 '13 at 07:36
  • 1
    > `Object value = get(column);` Unfortunately `get(int)` method is presented only in ICS, what should I do in 2.3? – Alex Bonel May 23 '13 at 07:40
  • Probably you want to start from the ICS implemantion. You can take the entire implemenation from grepcode, and you can try to adapt for your purpose. – Blackbelt May 23 '13 at 07:44
  • Do you mean that to maintain the work of this code in versions pre ICS I should copy the entire code of ICS's `MatrixCursor`? – Alex Bonel May 23 '13 at 07:52
  • Is it normal behaviour in android to maintain the code in a such way? I just want to know, because I think, that you have really bigger experience than myself in android development considering your SO reputation. – Alex Bonel May 23 '13 at 07:54
  • 1
    I dislike it also, but if you need things to work on pre ICS it could be a valid workaround. Take it as last resort. In your case, instead of getting the blob data you can store your image as a Base64 string. – Blackbelt May 23 '13 at 07:58
  • Base64 is nice workaround too. And unfortunatelly, I cannot copy ICS's `MatrixCursor` implementation, because it uses `DatabaseUtils.getTypeOfObject(Object)` method which is `static public` in the source code, but somehow can't be accessed from the code as Eclipse says and it's not mentioned in documentation. [Here](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.2_r1/android/database/DatabaseUtils.java#DatabaseUtils.getTypeOfObject%28java.lang.Object%29) is the code – Alex Bonel May 23 '13 at 08:03
  • Bigger 1.37 times than original image – Alex Bonel May 23 '13 at 08:06
  • Yes it is bigger than original 1.37 times – Alex Bonel May 23 '13 at 08:07
  • and how do you compare its size ? And then on what do you compare it? – Blackbelt May 23 '13 at 08:09
  • I just took this information from [wikipedia](http://en.wikipedia.org/wiki/Base64#Implementations_and_history) and form [this](http://stackoverflow.com/questions/11402329/base64-encoded-image-size) post – Alex Bonel May 23 '13 at 08:11
  • I see . Can you save the image on the disk and put the path into the database? – Blackbelt May 23 '13 at 08:15
  • Silly me, I just take the logic from `DatabaseUtils.getTypeOfObject(Object)` and placed it to my extended `MatrixCursor.getType(int)` method. Now I at least see list items but they are not inflated. Will try to understand why, thanks for the help – Alex Bonel May 23 '13 at 08:23