Android ExpandableListView和SQLite数据库

前端之家收集整理的这篇文章主要介绍了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"
                + ");");
    }

解决方法

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

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,long id) {
        // Your group click code here
        return true;
    }
});
原文链接:https://www.f2er.com/android/316883.html

猜你在找的Android相关文章