ExpandableListView具有来自各个表的数据 [英] ExpandableListView with data from various tables

查看:55
本文介绍了ExpandableListView具有来自各个表的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我的SQLite数据库中的数据制作一个可扩展的列表视图.

I want to make an expandable list view with data from my SQLite database.

但是我只想从各个表中获取子列表的数据,并且组将始终相同.

But I only want to take data for child lists from various tables, and Groups will be always the same.

然后将每个表的数据放入相关组的子列表中.

And then put the data of each table to child list of related group.

有可能吗?有例子吗?

谢谢.

推荐答案

好吧,请稍做介绍.我从此示例中提取的项目是购物清单应用程序.可以有多个购物清单,其中包含不同的项目和数量.

Ok, a little lead in. The project I'm pulling this example from is a shopping list app. There can be multiple shopping lists, with different items and quantities.

因此,正在使用三个表.杂货清单表,它只是一个具有ID和名称的表.然后是一个包含ID,项目名称,计量单位,类别,过道和价格的项目表.最终表是列表表,其中包含ID,列表ID,项目ID和列表中的数量.

So, three tables in use. The grocery list table, which is just a table with an id and a name. Then an items table that contains the id, item name, unit of measure, category, aisle and price. The final table is the list table, which contains the id, the list id, the item id and the quantity in the list.

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

I wanted to be able to sort by category or aisle, so I created some cursor methods that take parameters to specify which column to use.

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

Three things make this work... first you use DISTINCT in the group cursor query to make sure you get one record for each seperate item you want to group on. Second, you set them as the _id. Finally, you JOIN the various tables to get the info you need into a single cursor.

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));
    }
}

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

Hopefully this illustrates what you need. If you have any questions, leave me a comment and I'll try and clarify.

这篇关于ExpandableListView具有来自各个表的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆