Approach to populate the Expandable List View with local SQlite database

China☆狼群 提交于 2019-12-04 02:55:21

I had created a project to try out expandablelistview and database, hope this helps

 final class ExpAdapter extends CursorTreeAdapter {
        LayoutInflater mInflator;

        public ExpAdapter(Cursor cursor, Context context) {
            super(cursor, context);
            mInflator = LayoutInflater.from(context);
        }

        @Override
        protected void bindChildView(View view, Context context, Cursor cursor,
                boolean isLastChild) {
            TextView tvChild = (TextView) view.findViewById(android.R.id.text1);
            tvChild.setText(cursor.getString(cursor
                    .getColumnIndex(DBHelper.COL_TABLE_CHILD_NAME)));
        }

        @Override
        protected void bindGroupView(View view, Context context, Cursor cursor,
                boolean isExpanded) {
            TextView tvGrp = (TextView) view.findViewById(android.R.id.text1);
            tvGrp.setText(cursor.getString(cursor
                    .getColumnIndex(DBHelper.COL_TABLE_MAIN_NAME)));
        }

        @Override
        protected Cursor getChildrenCursor(Cursor groupCursor) {
            int groupId = groupCursor.getInt(groupCursor
                    .getColumnIndex(DBHelper.COL_ID));
            return aDBHelper.getChildCursor(groupId);
        }

        @Override
        protected View newChildView(Context context, Cursor cursor,
                boolean isLastChild, ViewGroup parent) {
            View mView = mInflator.inflate(
                    android.R.layout.simple_expandable_list_item_1, null);
            TextView tvChild = (TextView) mView
                    .findViewById(android.R.id.text1);
            tvChild.setText(cursor.getString(cursor
                    .getColumnIndex(DBHelper.COL_TABLE_CHILD_NAME)));
            return mView;
        }

        @Override
        protected View newGroupView(Context context, Cursor cursor,
                boolean isExpanded, ViewGroup parent) {
            View mView = mInflator.inflate(
                    android.R.layout.simple_expandable_list_item_1, null);
            TextView tvGrp = (TextView) mView.findViewById(android.R.id.text1);
            tvGrp.setText(cursor.getString(cursor
                    .getColumnIndex(DBHelper.COL_TABLE_MAIN_NAME)));
            return mView;
        }

    }

You don't have to create a content provider for that.

  1. Create a DBHelper class and create functions for fetching records from your local database without a content provider.
  2. Create a model class for holding the records from your local database.
  3. Use this model class as your data for your list adapter.

If your having local database, best approach is to go with CursorTreeAdapter because it automatically handles updating the list.

Check this post my help you. ExpandableListView using CursorTreeAdapter with in fragment, Content Provider, LoaderCallbacks and CursorLoader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!