How to group rows in a list and give a title when fetching data from a DB
I got a dialog snippet in my application that lists some values like: dresses from a table in a database. I want to display a list in such a way that " values are grouped ". those. the list should appear with headings of men, women, children and dresses for them under each heading. Also in the database there is a column containing these values for men, women, children . Therefore, by referring to this column, the list must be sorted.
source to share
Like others ExpandableListView
have suggested , you need to use by providing cursors with appropriate data. You have not specified your database schema, so I am assuming that you only have one table with clothes, and that table also has (besides the names of clothes and other data) a type column where you put a person, woman, child, etc. .d.
You can do what you want by first querying the database to retrieve 3 types of clothes and then return the cursor holding the clothes for that specific type in the adapter. Below is an example of how you can 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 custom adapter handles getting child data for each garment type:
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
}
}
source to share