正如其他人所说,您需要使用ExpandableListView 为游标提供适当的数据。你没有提到你的数据库模式,所以我假设你只有一个带有衣服的表,并且这个表还有(除了衣服名称和其他数据)你放置男人、女人、孩子等的类型列。
您可以做您想做的事,首先查询数据库以检索 3 种衣服类型,然后在适配器中返回包含该特定类型衣服的光标。下面是一个关于如何做到这一点的示例:
// 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));
以下是用于处理检索每种衣服的子数据的自定义适配器:
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
}
}