I have pictures in my sqlite prepopulated database, i'm using Simplecursortreeadapter to pass data to exepndablelistview , but the app crush when opening a childview with a picture and in logcat i got "UNABLE TO CONVERT STRING TO BLLOB'. After making some research on internet i discovered(maybe) i have to implement viewbinder which i did ( copy past and addapt to my columns) ..what i have done is based on the answer of this question:Unable to convert BLOB to String using Loadermanager in android ..
But The app can't build successfuly now...i think i'm missing something or there is something wrong with my code:
Please help! thanks
public class MainActivity extends AppCompatActivity {
ExpandableListView expandableListView;
Database mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatabase = new Database(this);
mDatabase.open();
SimpleCursorTreeAdapter.setViewBinder( new MyViewBinder());
Cursor cursor = mDatabase.getDatabase();
startManagingCursor(cursor);
String[] childFrom = new String[]{Database.DATABASE_CHILD_1,Database.DATABASE_CHILD_2};
String[] groupFrom = new String[]{Database.DATABASE_GROUP_1};
int[] groupTo = {R.id.group1};
int[] childTo = {R.id.child1,R.id.child2};
SimpleCursorTreeAdapter simplecursortreeAdapter = new ExpandableListViewAdapter(
this,
cursor,
R.layout.list_group,
groupFrom,
groupTo,
R.layout.list_child,
childFrom,
childTo
);
expandableListView = findViewById(R.id.expandableListview);
expandableListView.setAdapter(simplecursortreeAdapter);
}
protected void onDestroy() {
super.onDestroy();
mDatabase.close();
}
public class MyViewBinder implements ViewBinder {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int viewID = view.getId();
switch(viewID){
case R.id.child1 :
TextView friendName = (TextView) view;
String friend_name;
friend_name = cursor.getString(cursor.getColumnIndex(Database.DATABASE_CHILD_1));
friendName.setText(friend_name);
break;
case R.id.child2 :
ImageView contactProfile = (ImageView) view;
byte[] imageBytes = cursor.getBlob(cursor.getColumnIndex(Database.DATABASE_CHILD_2));
if(imageBytes != null ){
// Pic image from database
contactProfile.setImageBitmap(BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length));
}else {
// If image not found in database , assign a default image
contactProfile.setBackgroundResource(R.drawable.disorders);
}
break;
}
return true;
}
}
private class ExpandableListViewAdapter extends SimpleCursorTreeAdapter {
private ExpandableListViewAdapter(
Context context,
Cursor cursor,
int groupLayout,
String[] groupFrom,
int[] groupTo,
int childLayout,
String[] childFrom,
int[] childTo) { super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childFrom, childTo); }
protected Cursor getChildrenCursor(Cursor groupCursor) {
return mDatabase.getID(groupCursor.getInt(groupCursor.getColumnIndex(Database.DATABASE_ID)));
}
}
}
i found the answer (the problem was i have misplaced
SimpleCursorTreeAdapter.setViewBinder( new MyViewBinder());
..