android - 具有来自各种表的数据的 ExpandableListView

标签 android expandablelistview sqlite

我想用我的 SQLite 数据库中的数据制作一个可扩展的 ListView 。

但我只想从各个表中获取子列表的数据,而组将始终相同。

然后将每个表的数据放到相关分组的child list中。

这可能吗?有例子吗?

谢谢。

最佳答案

好吧,有点引导。我从这个例子中提取的项目是一个购物 list 应用程序。可以有多个购物 list ,其中包含不同的项目和数量。

因此,使用了三个表。杂货 list 表,它只是一个带有 ID 和名称的表。然后是一个项目表,其中包含 ID、项目名称、计量单位、类别、过道和价格。最终表为list表,包含id,list id,item id和list中的数量。

我希望能够按类别或过道进行排序,因此我创建了一些游标方法,这些方法采用参数来指定要使用的列。

三件事使这项工作...首先,您在组游标查询中使用 DISTINCT 以确保您为每个要分组的单独项目获得一条记录。其次,将它们设置为 _id。最后,您JOIN 各种表以将您需要的信息放入单个游标中。

dbhelper 可扩展列表游标

public Cursor fetchGroup(String group, long list) {
    String query = "SELECT DISTINCT "
            + group
            + " AS _id FROM "
            + LISTITEM_TABLE
            + " JOIN "
            + ITEM_TABLE
            + " ON list_items.item_id=items._id WHERE list_items.list_id = "
            + list;
    return mDb.rawQuery(query, null);
}

public Cursor fetchChildren(String column, String token, long list) {
    String query = "SELECT list_items._id, list_items.item_qty, items.name, items.cost, items.unit, list_items.checked FROM list_items JOIN items ON list_items.item_id=items._id WHERE "
            + column
            + "='"
            + token
            + "' AND list_items.list_id = "
            + list
            + " ORDER BY list_items.checked, items.name";
    return mDb.rawQuery(query, null);
}

设置可扩展列表

    ExpandableListView lv;
    mGroupsCursor = mDbHelper.fetchGroup(group,
            ListFragment_ShopList.listId);
    getActivity().startManagingCursor(mGroupsCursor);
    mGroupsCursor.moveToFirst();

    lv = (ExpandableListView) getActivity().findViewById(android.R.id.list);

    mAdapter = new MyExpandableListAdapter(mGroupsCursor, getActivity(),
            R.layout.rowlayout_expgroup, R.layout.rowlayout_itemlist_exp,
            new String[] { "_id" }, new int[] { android.R.id.text1 },
            new String[] { GroceryDB.ITEM_NAME, GroceryDB.LISTITEM_QTY,
                    GroceryDB.ITEM_UNIT }, new int[] { R.id.ListItem1,
                    R.id.ListItem3, R.id.ListItem2 });
    lv.setAdapter(mAdapter);

public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {

    public MyExpandableListAdapter(Cursor cursor, Context context,
            int groupLayout, int childLayout, String[] groupFrom,
            int[] groupTo, String[] childrenFrom, int[] childrenTo) {
        super(context, cursor, groupLayout, groupFrom, groupTo,
                childLayout, childrenFrom, childrenTo);
    }

    @Override
    protected Cursor getChildrenCursor(Cursor groupCursor) {
        Cursor childCursor = mDbHelper.fetchChildren(GroceryListMain.group,
                groupCursor.getString(groupCursor.getColumnIndex("_id")),
                ListFragment_ShopList.listId);
        getActivity().startManagingCursor(childCursor);
        childCursor.moveToFirst();
        return childCursor;
    }

    protected void bindChildView(View view, Context context, Cursor cursor,
            boolean isLastChild) {

        TextView name = (TextView) view.findViewById(R.id.ListItem1);
        TextView qty = (TextView) view.findViewById(R.id.ListItem3);
        TextView unit = (TextView) view.findViewById(R.id.ListItem2);
        TextView cost = (TextView) view.findViewById(R.id.ListItem4);

        name.setTextColor(MyApplication.shoplistitem_name);
        unit.setTextColor(MyApplication.shoplistitem_desc);
        qty.setTextColor(MyApplication.shoplistitem_qty);
        cost.setTextColor(MyApplication.shoplistitem_desc);

        name.setText(cursor.getString(2));
        qty.setText(cursor.getString(1));
        unit.setText(cursor.getString(4));

        DecimalFormat df = new DecimalFormat("0.00");
        Double hold = Double.valueOf(cursor.getString(3));
        Double qtyhold = Double.valueOf(cursor.getString(1));
        Double total = hold * qtyhold;

        cost.setText("$" + df.format(total));
    }
}

希望这能说明您的需求。如果您有任何疑问,请给我留言,我会尽力澄清。

关于android - 具有来自各种表的数据的 ExpandableListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11450119/

相关文章:

android - fragment 变为可见时的监听器

android - 对话 fragment : NullPointerException (support library)

android - 自定义 ExpandableListview,其中包含子项中的另一个展开按钮

java - 有没有办法将 ExpandableListView 嵌套在relativelayout 中?

sql - 提高 Ruby 脚本处理 CSV 的性能

java - 无法在 android 中插入 SQLite 的值

Android 和 SQLite - 检索表的最大 ID

android - 将标题添加到 Android ListView

android - 如何使用 android 的可扩展 ListView 显示文件夹和文件列表?

sql - 查找所有用户共有的项目