我在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)));
}
}
}
答案 0 :(得分:0)
我找到了答案(问题是我放错了SimpleCursorTreeAdapter.setViewBinder( new MyViewBinder());
..
SimpleCursorTreeAdapter simplecursortreeAdapter = new ExpandableListViewAdapter(
this,
cursor,
R.layout.list_group,
groupFrom,
groupTo,
R.layout.list_child,
childFrom,
childTo
);
simplecursortreeAdapter.setViewBinder(new MyViewBinder());
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.group1 :
TextView groupName = (TextView) view;
String groupname;
groupname = cursor.getString(cursor.getColumnIndex(Database.DATABASE_GROUP_1));
groupName.setText(groupname);
break;
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)));
}
}
}