Hi I'm having a few problems with view finder and was wondering if anyone could help.
I have an SQLite database which is fine and am storing small thumbnail images in it as blobs. I know the blobs are being saved as I am able to retrieve them in another area of my application.
Now, I'm trying to use ViewBinder to bind an image from the database to my custom list view. ( you can think of it as a contact manager sort of layout where I have a list of names and numbers with a corresponding image.)
I have tried a few different methods including using a simple cursor adapter and from my research it seems that creating my own view binder seemed the way to do it. The code has no errors however at run time I'm getting a ClassCastException.
Below is the code from my main list activity
listViewCards = (ListView) findViewById(android.R.id.list);
Cursor cursor = dbhelper.getAllContacts();
SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(
getApplicationContext(),
R.layout.listview_each_item,
cursor,
new String[] { SQLiteAdapter.KEY_NAME,SQLiteAdapter.KEY_PHONE, SQLiteAdapter.KEY_COMPANYNAME},
new int[] { R.id.list_item_name,R.id.list_item_phone, R.id.list_item_companyname },0);
ImageView image = (ImageView) findViewById(R.id.list_item_image);
MyViewBinder mvb = new MyViewBinder();
mvb.setViewValue(image, cursor, cursor.getColumnIndex(SQLiteAdapter.KEY_IMAGE));
myAdapter.setViewBinder(mvb);
listViewCards.setAdapter(myAdapter);
and for my custom view binder
public class MyViewBinder implements ViewBinder{
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
ImageView image = (ImageView) view;
cursor.moveToFirst();
byte[] byteArray = cursor.getBlob(columnIndex);
if(image !=null){
image.setImageBitmap(BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length));
return false;
}
return true;
}
Now the logcat
08-14 11:01:15.275: E/AndroidRuntime(2669): FATAL EXCEPTION: main
08-14 11:01:15.275: E/AndroidRuntime(2669): java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ImageView
08-14 11:01:15.275: E/AndroidRuntime(2669): at com .example.npacards.MyViewBinder.setViewValue(MyViewBinder.java:14)
08-14 11:01:15.275: E/AndroidRuntime(2669): at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:146)
08-14 11:01:15.275: E/AndroidRuntime(2669): at android.widget.CursorAdapter.getView(CursorAdapter.java:250)
08-14 11:01:15.275: E/AndroidRuntime(2669): at android.widget.AbsListView.obtainView(AbsListView.java:2457)
08-14 11:01:15.275: E/AndroidRuntime(2669): at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
08-14 11:01:15.275: E/AndroidRuntime(2669): at android.widget.ListView.onMeasure(ListView.java:1162)
08-14 11:01:15.275: E/AndroidRuntime(2669): at android.view.View.measure(View.java:15473)
08-14 11:01:15.275: E/AndroidRuntime(2669): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
08-14 11:01:15.275: E/AndroidRuntime(2669): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
Line 14 in my view binder is
ImageView image = (ImageView) view;
Could anyone help me understand why this produces a classCastExeption as the view that is being passed into my view binder is
ImageView image = (ImageView) findViewById(R.id.list_item_image);
Any ideas are much appreciated.