A ViewBinder
will get called for every view declared in the to
(adapter's constructor) array passed when creating the adapter. Right now you're not passing the ImageView
(through it's id) so the ViewBinder
will not get called for the ImageView
, it will only get called for the views with the ids R.id.list_item_name
, R.id.list_item_phone
and R.id.list_item_companyname
. This is why you get that ClassCastException
, because you wrongly assume that the view passed to the ViewBinder
is the ImageView
(this is another mistake you made, because you don't look at the ids of the views passed to the ViewBinder
to see which view is set to be binded at that moment with data from the Cursor
).
To solve it you need to let the adapter know that you also want the ImageView
from the row to get binded(notice the extra column and id):
SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(
getApplicationContext(),
R.layout.listview_each_item,
cursor,
new String[] { SQLiteAdapter.KEY_NAME,SQLiteAdapter.KEY_PHONE, SQLiteAdapter.KEY_COMPANYNAME, SQLiteAdapter.KEY_IMAGE},
new int[] { R.id.list_item_name,R.id.list_item_phone, R.id.list_item_companyname, R.id.list_item_image},0);
Then change the ViewBinder
to handle setting the image, letting the adapter bind the other views on its own:
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == R.id.list_item_image) { // we have our ImageView so bind the image
byte[] byteArray = cursor.getBlob(columnIndex);
((ImageView)view).setImageBitmap(BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length));
return true; // return true to let the adapter know that we handled this view on our own
}
return false; // return false for any other view so the adapter will bind the data on its own
}
This:
mvb.setViewValue(image, cursor, cursor.getColumnIndex(SQLiteAdapter.KEY_IMAGE));
is incorrect. You don't call setViewValue()
on your own, the adapter will call it in the getView()
method for each of the views with the ids from the to
array to set the data on them.