Scarmysun Scarmysun - 2 years ago 146
Android Question

Images in SimpleCursorAdapter

I'm trying to use a

with a
to get an image from the database and put it into my
item view. Here is my code:

private void setUpViews() {
mNewsView = (ListView) findViewById(;

Cursor cursor = getNews();
SimpleCursorAdapter curAdapter = new SimpleCursorAdapter(
getApplicationContext(), R.layout.cursor_item, cursor,
new String[] { "title", "content", "image" },
new int[] {,, });

ViewBinder viewBinder = new ViewBinder() {

public boolean setViewValue(View view, Cursor cursor,
int columnIndex) {
ImageView image = (ImageView) view;
byte[] byteArr = cursor.getBlob(columnIndex);
image.setImageBitmap(BitmapFactory.decodeByteArray(byteArr, 0, byteArr.length));
return true;
ImageView image = (ImageView) findViewById(;
viewBinder.setViewValue(image, cursor, cursor.getColumnIndex("image"));

I am getting a :

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 60

while executing
byte[] byteArr = cursor.getBlob(columnIndex);
. Does anyone have an idea what am I doing wrong?

Answer Source

I think the cursor.moveToFirst() has not been called so the cursor is throwing android.database.CursorIndexOutOfBoundsException.

Before using a cursor you should always check is the cursor is empty or not by calling cursor.moveToFirst(). This will also position the cursor at the first position.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download