我想用我的SQLite數據庫中的數據製作一個可擴展列表視圖。ExpandableListView來自各種表格的數據
但我只想從各種表中獲取子列表的數據,並且組將始終保持不變。
然後將每個表的數據放到相關組的子列表中。
有可能嗎?有一些例子嗎?
謝謝。
我想用我的SQLite數據庫中的數據製作一個可擴展列表視圖。ExpandableListView來自各種表格的數據
但我只想從各種表中獲取子列表的數據,並且組將始終保持不變。
然後將每個表的數據放到相關組的子列表中。
有可能嗎?有一些例子嗎?
謝謝。
好吧,稍微領先一點。我從這個例子中抽取的項目是一個購物清單應用程序。可以有多個購物清單,具有不同的物品和數量。
所以,三個表在使用。購物清單表,這只是一個ID和名稱的表。然後是一個物品表,其中包含ID,物品名稱,計量單位,類別,通道和價格。最終表是列表,其中包含ID,列表ID,項目ID和列表中的數量。
我希望能夠按類別或通道進行排序,因此我創建了一些遊標方法,它們通過參數指定要使用的列。
三樣東西,使這項工作......首先你使用DISTINCT在小組光標查詢,以確保你得到一個記錄要在組中的每個單獨的項目。其次,你將它們設置爲_id
。最後,你可以通過各種表格來獲得你需要的信息到一個單一的遊標中。
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));
}
}
希望這說明了你所需要的。如果您有任何問題,請留下評論,我會盡力澄清。
什麼是組,ListFragment_ShopList.listId)在您的代碼行。 – tazeenmulani 2014-06-23 05:46:52
這很可能。如果有人不打我,我會回到家後舉個例子。 – Barak 2012-07-12 12:03:28
謝謝巴拉克。 – user1520501 2012-07-12 12:31:23
沒問題,它是否適合你? – Barak 2012-07-13 12:57:10