首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用viewbinder Simplecursortreeadapter -unable将字符串转换为blob时出现问题

使用viewbinder Simplecursortreeadapter -unable将字符串转换为blob时出现问题
EN

Stack Overflow用户
提问于 2018-08-01 02:44:53
回答 1查看 33关注 0票数 0

我在我的sqlite预填充数据库中有图片,我正在使用Simplecursortreeadapter来传递数据到exepndablelistview,但是当打开一个带有图片的子视图时,应用程序崩溃了,在logcat中我得到了"UNABLE TO CONVERT STRING TO BLLOB“。在互联网上做了一些研究后,我发现(也许)我必须实现我所做的视图绑定器(复制过去并添加到我的列中) ..what我所做的是基于这个问题的答案:Unable to convert BLOB to String using Loadermanager in android

但是应用程序现在不能成功构建...我想我遗漏了什么,或者我的代码有问题:

请帮帮我!谢谢

代码语言:javascript
复制
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)));
        }

    }


}
EN

回答 1

Stack Overflow用户

发布于 2018-08-01 05:27:04

我找到了答案(问题是我把SimpleCursorTreeAdapter.setViewBinder( new MyViewBinder());放错地方了。

代码语言:javascript
复制
     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)));
        }

    }


}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51620110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档