【问题标题】:How to Group rows in List and give heading , when fetching Data from DB从数据库中获取数据时如何对列表中的行进行分组并给出标题
【发布时间】:2014-11-04 00:21:11
【问题描述】:

我的应用程序中有一个对话框片段,其中列出了一些值,例如:数据库中表中的连衣裙。我想以“值被分组”的方式显示列表。即列表应该出现在每个标题下方的标题男性、女性、儿童和他们的衣服。 另外在数据库中有一列包含这些 men,women,kids 值。所以引用该列,列表应该被排序。

【问题讨论】:

  • @aga 这对我来说非常困难,因为我是初学者,我想以编程方式更新列表和标题。因为我正在从数据库中获取值
  • 到底你想要一个 ExpandableListView(可折叠组)的行为还是你想要一些 extra 行作为标题,这些标题下的值分组?
  • @Luksprog 的 ExpandableListView 行为(可折叠组)

标签: android list sqlite android-dialogfragment


【解决方案1】:

正如其他人所说,您需要使用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
    }
}

【讨论】:

    猜你喜欢
    • 2021-05-23
    • 2012-09-30
    • 1970-01-01
    • 2019-11-13
    • 2019-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    相关资源
    最近更新 更多