我有两个表在我的数据库 - 房间和设备。每间客房都可以有许多设备。我想提出一个可扩展的ListView与房间名组和设备的名称和状态,儿童(或关闭)。
任何一个可以给我一个简单的想法,因为我是一种新的与Android和所有的教程是很难做出
这是我的数据库:
公共无效的onCreate(SQLiteDatabase DB){
如果不存在房db.execSQL(CREATE TABLE(
id_room整数主键自动增量,
+name_room文字不为空
+););
如果不存在设备db.execSQL(CREATE TABLE(
id_device整数主键自动增量,
+name_device文字不为空,
+state_device文字不为空,
+id_room引用房
+););
}
解决方案
以下是一个非常简单的,没有多余的装饰扩展列表实现。
您需要在您的分贝辅助类两种方法来收集你需要的光标。
公开光标fetchGroup(){
查询字符串=SELECT * FROM房
返回mDb.rawQuery(查询,NULL);
}
公共光标fetchChildren(字符串房){
查询字符串=SELECT * FROM设备WHERE id_room ='+房+';
返回mDb.rawQuery(查询,NULL);
}
然后,你需要设置你的适配器(在您的活动):
公共类MyExpandableListAdapter扩展SimpleCursorTreeAdapter {
公共MyExpandableListAdapter(光标指针,上下文语境,诠释的GroupLayout,
INT childLayout,字符串[] groupFrom,INT [] groupTo,字符串[] childrenFrom,
INT [] childrenTo){
超(背景下,光标的GroupLayout,groupFrom,groupTo,
childLayout,childrenFrom,childrenTo);
}
}
@覆盖
受保护的光标getChildrenCursor(光标groupCursor){
光标childCursor = mDbHelper.fetchChildren(groupCursor.getString(groupCursor.getColumnIndex(id_room));
。getActivity()startManagingCursor(childCursor);
childCursor.moveToFirst();
返回childCursor;
}
}
最后调用适配器并将其设置为您的列表(在你的活动):
私人无效fillData(){
mGroupsCursor = mDbHelper.fetchGroup();
。getActivity()startManagingCursor(mGroupsCursor);
mGroupsCursor.moveToFirst();
ExpandableListView ELV =(ExpandableListView)getActivity()findViewById(android.R.id.list)。
mAdapter =新MyExpandableListAdapter(mGroupsCursor,getActivity()
R.layout.rowlayout_expgroup,//你排布置为一组
R.layout.rowlayout_itemlist_exp,//你行布局为孩子
新的String [] {id_room} //场(S)从组光标使用
新的INT [] {} android.R.id.room,//控件的ID将数据分组到
新的String [] {name_device,state_device} //场(S)从子游标使用
新的INT [] {R.id.device,R.id.state}); //控件的ID将子数据成
lv.setAdapter(mAdapter); //设置列表适配器。
}
}
希望这有助于!
修改
这都应该走到一起,像这样:
公共类List_Exp延伸活动{
@覆盖
公共无效onActivityCreated(包savedInstanceState){
super.onActivityCreated(savedInstanceState);
mDbHelper =新YourDB(getActivity());
mDbHelper.open();
fillData();
}
私人无效fillData(){
//设置列表在这里适配器
}
公共类MyExpandableListAdapter扩展SimpleCursorTreeAdapter {
//你的适配器
}
}
编辑2
要赶点击,集听众对你的活动:
lv.setOnChildClickListener(新ExpandableListView.OnChildClickListener(){
@覆盖
公共布尔onChildClick(ExpandableListView父,视图V,
INT groupPosition,诠释childPosition,长ID){
//你的孩子点击code在这里
返回true;
}
});
lv.setOnGroupClickListener(新ExpandableListView.OnGroupClickListener(){
@覆盖
公共布尔onGroupClick(ExpandableListView父,视图V,
INT groupPosition,诠释groupPosition,长ID){
//你的组点击code在这里
返回true;
}
});
I have two tables in my database - rooms and devices. Each room can have many devices. I want to make an expandable listview with room name as group and devices name and state as children (on or off).
Can any one give me a simple idea because I am kind of new with Android and all tutorials are hard to make
This is my database:
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"
+ ");");
}
解决方案
Following is a very simple, no frills expandable list implementation.
You need two methods in your db helper class to gather the cursors you will need.
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);
}
Then you need to set up your adapter (in your activity):
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;
}
}
And finally call the adapter and set it to your list (in your activity):
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.
}
}
Hope this helps!
EDIT
It should all come together like so:
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
}
}
EDIT 2
To catch clicks, set listeners in you activity:
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;
}
});