Android ExpandableListView和SQLite数据库

我在我的数据库中有两个表格 – 房间和设备。 每个房间可以有很多设备。 我想做一个可扩展的列表视图与房间名称作为组和设备的名称和状态作为孩子(开或关)。

任何人都可以给我一个简单的想法,因为我是一种新的Android和所有的教程很难做

这是我的数据库:

public void onCreate(SQLiteDatabase db) { db.execSQL("create table if not exists rooms ( id_room integer primary key autoincrement, " + "name_room text not null" + ");"); db.execSQL("create table if not exists devices ( id_device integer primary key autoincrement, " + "name_device text not null," + "state_device text not null," + "id_room references rooms" + ");"); } 

Solutions Collecting From Web of "Android ExpandableListView和SQLite数据库"

以下是一个非常简单,没有多余的可扩展列表实现。

在你的db helper类中需要两个方法来收集你需要的游标。

 public Cursor fetchGroup() { String query = "SELECT * FROM rooms" return mDb.rawQuery(query, null); } public Cursor fetchChildren(String room) { String query = "SELECT * FROM devices WHERE id_room = '" + room + "'"; return mDb.rawQuery(query, null); } 

然后你需要设置你的适配器(在你的活动中):

 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(groupCursor.getString(groupCursor.getColumnIndex("id_room")); getActivity().startManagingCursor(childCursor); childCursor.moveToFirst(); return childCursor; } } 

最后,调用适配器并将其设置为您的列表(在您的活动中):

 private void fillData() { mGroupsCursor = mDbHelper.fetchGroup(); getActivity().startManagingCursor(mGroupsCursor); mGroupsCursor.moveToFirst(); ExpandableListView elv = (ExpandableListView) getActivity().findViewById(android.R.id.list); mAdapter = new MyExpandableListAdapter(mGroupsCursor, getActivity(), R.layout.rowlayout_expgroup, // Your row layout for a group R.layout.rowlayout_itemlist_exp, // Your row layout for a child new String[] { "id_room" }, // Field(s) to use from group cursor new int[] { android.R.id.room }, // Widget ids to put group data into new String[] { "name_device", "state_device" }, // Field(s) to use from child cursors new int[] { R.id.device, R.id.state }); // Widget ids to put child data into lv.setAdapter(mAdapter); // set the list adapter. } } 

希望这可以帮助!

编辑

它应该像这样一起走到一起:

 public class List_Exp extends Activity { @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mDbHelper = new YourDB(getActivity()); mDbHelper.open(); fillData(); } private void fillData() { // set list adapter here } public class MyExpandableListAdapter extends SimpleCursorTreeAdapter { // Your adapter } } 

编辑2

要捕捉点击,请在您的活动中设置侦听器:

 lv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // Your child click code here return true; } }); lv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, int groupPosition, long id) { // Your group click code here return true; } });