【发布时间】:2018-07-31 18:44:53
【问题描述】:
我的 sqlite 预填充数据库中有图片,我正在使用 Simplecursortreeadapter 将数据传递给 exepndablelistview ,但是当打开带有图片的子视图并且在 logcat 中我得到“无法将字符串转换为 BLLOB”时,应用程序崩溃了。 在对互联网进行了一些研究之后,我发现(也许)我必须实现我所做的 viewbinder(复制过去并添加到我的专栏中)..我所做的是基于这个问题的答案:Unable to convert BLOB to String using Loadermanager in android..
但是应用程序现在无法成功构建...我想我遗漏了一些东西或者我的代码有问题:
请帮忙!谢谢
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)));
}
}
}
【问题讨论】:
-
@Deepak Rathore,你能帮帮我吗?谢谢
标签: android simplecursortreeadapter