Search code examples
androidlistsqliteandroid-dialogfragment

How to Group rows in List and give heading , when fetching Data from DB


I got a dialog fragment in my app, which lists some values eg :dresses from a table in database. I want to show the list in such a way that "the values are grouped". ie the list should appear with headings men, women, kids and the dresses for them below each heading. Also in the database there is a column containing these men,women,kids values. So referring that column the list should be sorted.


Solution

  • As other said you need to use an ExpandableListView providing the cursors with the appropriate data. You didn't mention your database schema so I'm assuming that you have only one table with the clothes and this table also has(besides the clothes names and other data) the type column where you put man, woman, child etc.

    You could do what you want by first querying the database to retrieve the 3 types of clothes followed by returning the cursor holding the clothes for that specific type in the adapter. Below is an example on how you might do this:

    // I'm assumming that your database table is called "clothes" and the 
    // column holding the type is called "type"
    SQLiteDatabase db= /*get the database*/;
    // this query will return a cursor containing 3 rows(for man, woman, child)
    Cursor typesCursor = db.rawQuery("SELECT * FROM clothes GROUP BY type", null);
    expandableListViewWidget.setAdapter(new CustomAdapter(typesCursor, this, db));
    

    Below is the custom adapter to handle retrieving the child data for each type of clothes:

    public static class CustomAdapter extends CursorTreeAdapter {
    
        private SQLiteDatabase mDb;
        private LayoutInflater mInflater;
    
        public CustomAdapter(Cursor cursor, Context context, SQLiteDatabase db) {
            super(cursor, context);
            mDb = db;
            mInflater = LayoutInflater.from(context);
        }
    
        @Override
        protected Cursor getChildrenCursor(Cursor groupCursor) {
            // find which type of clothes you're dealing with and return the data only for that type
            final String type = groupCursor.getString(groupCursor.getColumnIndex("type"));
            return mDb.rawQuery("SELECT * FROM clothes WHERE type=?", new String[]{type});
        }
    
        @Override
        protected View newGroupView(Context context, Cursor cursor, boolean isExpanded, ViewGroup parent) {
            // here you'll return the view for the group rows
            View v = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
            return v;
        }
    
        @Override
        protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) {
            // here you'll bind the type of clothes to the group view 
            String type = cursor.getString(cursor.getColumnIndex("type"));
            // bind the data to the views
        }
    
        @Override
        protected View newChildView(Context context, Cursor cursor, boolean isLastChild, ViewGroup parent) {
            // here you'll return the view for the child rows  
            View v = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
            return v;
        }
    
        @Override
        protected void bindChildView(View view, Context context, Cursor cursor, boolean isLastChild) {
            String clothes = cursor.getString(cursor.getColumnIndex("clothes_name")); // assmuning that you keep the clothes name in this column
            // bind the data to the views
        }
    }