423,504 Members | 1,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,504 IT Pros & Developers. It's quick & easy.

Trouble with viewbinder simplecursortreeadapter -unable to convert string to blob

P: 1
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:https://stackoverflow.com/questions/30388843/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


Expand|Select|Wrap|Line Numbers
  1.     public class MainActivity extends AppCompatActivity {
  2.         ExpandableListView expandableListView;
  3.         Database mDatabase;
  4.  
  5.         @Override
  6.         protected void onCreate(Bundle savedInstanceState) {
  7.             super.onCreate(savedInstanceState);
  8.             setContentView(R.layout.activity_main);
  9.  
  10.             mDatabase = new Database(this);
  11.             mDatabase.open();
  12.  
  13.             SimpleCursorTreeAdapter.setViewBinder( new MyViewBinder());
  14.  
  15.             Cursor cursor = mDatabase.getDatabase();
  16.             startManagingCursor(cursor);
  17.  
  18.             String[] childFrom = new String[]{Database.DATABASE_CHILD_1,Database.DATABASE_CHILD_2};
  19.             String[] groupFrom = new String[]{Database.DATABASE_GROUP_1};
  20.  
  21.             int[] groupTo = {R.id.group1};
  22.             int[] childTo = {R.id.child1,R.id.child2};
  23.  
  24.             SimpleCursorTreeAdapter simplecursortreeAdapter = new ExpandableListViewAdapter(
  25.                     this,
  26.                     cursor,
  27.                     R.layout.list_group,
  28.                     groupFrom,
  29.                     groupTo,
  30.                     R.layout.list_child,
  31.                     childFrom,
  32.                     childTo
  33.             );
  34.  
  35.             expandableListView = findViewById(R.id.expandableListview);
  36.             expandableListView.setAdapter(simplecursortreeAdapter);
  37.         }
  38.  
  39.         protected void onDestroy() {
  40.             super.onDestroy();
  41.             mDatabase.close();
  42.         }
  43.  
  44.         public class MyViewBinder implements ViewBinder {
  45.             @Override
  46.             public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
  47.                 int viewID = view.getId();
  48.                 switch(viewID){
  49.                     case R.id.child1 :
  50.                         TextView friendName = (TextView) view;
  51.                         String friend_name;
  52.                         friend_name = cursor.getString(cursor.getColumnIndex(Database.DATABASE_CHILD_1));
  53.                         friendName.setText(friend_name);
  54.                         break;
  55.  
  56.                     case R.id.child2 :
  57.                         ImageView contactProfile = (ImageView) view;
  58.                         byte[] imageBytes = cursor.getBlob(cursor.getColumnIndex(Database.DATABASE_CHILD_2));
  59.                         if(imageBytes != null ){
  60.                             // Pic image from database
  61.                             contactProfile.setImageBitmap(BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length));
  62.                         }else {
  63.                             // If image not found in database , assign a default image
  64.                             contactProfile.setBackgroundResource(R.drawable.disorders);
  65.                         }
  66.                         break;
  67.                 }
  68.                 return true;
  69.             }
  70.  
  71.         }
  72.  
  73.         private class ExpandableListViewAdapter extends SimpleCursorTreeAdapter {
  74.             private ExpandableListViewAdapter(
  75.                     Context context,
  76.                     Cursor cursor,
  77.                     int groupLayout,
  78.                     String[] groupFrom,
  79.                     int[] groupTo,
  80.                     int childLayout,
  81.                     String[] childFrom,
  82.                     int[] childTo) { super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childFrom, childTo); }
  83.  
  84.             protected Cursor getChildrenCursor(Cursor groupCursor) {
  85.                 return mDatabase.getID(groupCursor.getInt(groupCursor.getColumnIndex(Database.DATABASE_ID)));
  86.             }
  87.  
  88.         }
  89.  
  90.  
  91.     }
  92.  
Jul 31 '18 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.