Approach to populate the Expandable List View with local SQlite database
A

3

9

I have a sqlite database in my application. I want to make an expandable list view with that.

I am fixed with the approach I should take for that.

Tried a lot to find a tutorial for the same, but could not find a single one, where one is populating the Expandable list with the local database.

There is this one tutorial in the android site where they are filling the expandable list with the Contact detail in the phone. They are doing this from the content provider ExpandableList2.java

So my question is should I also create a content provider for my database which is inside my own application ?

Is this the only approach or is there any other better one?

Agentival answered 20/7, 2011 at 11:9 Comment(2)
So what approach finally worked for you ? I am stuck in a similar problem also.Sandi
Have you got the solution?Zwieback
R
3

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;
        }

    }
Rawdon answered 21/7, 2011 at 6:58 Comment(3)
Thank you so much for your help. But looks like that I cannot use my custom view for the group and child with the CursorApadperTree. Is this true?? And so for this what should be used? Should I use SimpleCursorTreeAdapter for that? Any suggestionAgentival
I guess I should also use ExpandableListActivity. working on itAgentival
Thank you so much for your help, it wasn't so difficult.. thanks in tons :)Agentival
L
2

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.
Latrell answered 19/9, 2014 at 11:18 Comment(0)
L
0

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

Lickerish answered 20/9, 2014 at 5:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.