2012-05-17 161 views
7

我有兩個表在我的數據庫 - 房間和設備。每個房間可以有很多設備。我想做一個可擴展的列表視圖,將房間名稱作爲組和設備名稱和狀態作爲子項(打開或關閉)。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" 
       + ");"); 
    } 

回答

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

EDIT 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; 
    } 
}); 
+0

我必須刪除t他的getActivity()。startManagingCursor(childCursor)上的getChildrenCursor(),因爲我沒有任何解決方法,以消除錯誤的方法getActivity()是未定義的類型MyExpandableListAdapter! –

+0

啊,那個代碼是在一個片段中,所以它會在那裏工作。你可以嘗試使用'this'來代替,或者'youractivity.this'。 – Barak

+0

我試圖用這個,但是因爲你可以看到MyExpandableListAdapter()不是一個活動??! –