Android ExpandableListView和SQLite数据库

时间:2012-05-17 23:40:33

标签: android sqlite expandablelistview

我的数据库中有两个表 - 房间和设备。每个房间都有许多设备。我想制作一个可扩展的列表视图,其中房间名称为组,设备名称和状态为子项(打开或关闭)。

任何人都可以给我一个简单的想法,因为我对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"
                + ");");
    }

1 个答案:

答案 0 :(得分:13)

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

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