SQLite データベースのデータを使用して展開可能なリスト ビューを作成したいと考えています。
しかし、さまざまなテーブルから子リストのデータを取得したいだけで、グループは常に同じになります。
そして、各テーブルのデータを関連グループの子リストに入れます。
可能です?何か例はありますか?
ありがとう。
SQLite データベースのデータを使用して展開可能なリスト ビューを作成したいと考えています。
しかし、さまざまなテーブルから子リストのデータを取得したいだけで、グループは常に同じになります。
そして、各テーブルのデータを関連グループの子リストに入れます。
可能です?何か例はありますか?
ありがとう。
わかりました、少しリードインします。この例を引っ張っているプロジェクトは、買い物リスト アプリです。アイテムと数量が異なる複数のショッピング リストが存在する場合があります。
つまり、3 つのテーブルが使用されています。ID と名前を持つ単なるテーブルである食料品リスト テーブル。次に、ID、アイテム名、測定単位、カテゴリ、通路、および価格を含むアイテム テーブル。最後のテーブルは、ID、リスト ID、アイテム ID、およびリスト内の数量を含むリスト テーブルです。
カテゴリ別または通路別にソートできるようにしたかったので、使用する列を指定するパラメータを取るカーソル メソッドをいくつか作成しました。
3 つのことがこれを機能させます。最初に、グループ カーソル クエリで DISTINCT を使用して、グループ化する個別のアイテムごとに 1 つのレコードを取得するようにします。次に、それらを として設定します_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));
}
}
うまくいけば、これはあなたが必要とするものを示しています。ご不明な点がございましたら、コメントを残していただければ、明確にいたします。