Android ExpandableListView 和 SQLite 数据库 [英] Android ExpandableListView and SQLite Database

查看:35
本文介绍了Android ExpandableListView 和 SQLite 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

谁能给我一个简单的想法,因为我是 Android 新手,所有教程都很难制作

Can any one give me a simple idea because I am kind of new with Android and all tutorials are hard to make

这是我的数据库:

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.

您需要在 db helper 类中使用两个方法来收集您需要的游标.

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.
    }
}

希望这有帮助!

编辑

一切都应该像这样:

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

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

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

这篇关于Android ExpandableListView 和 SQLite 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆