2012-05-17 42 views
7

Tôi có hai bảng trong cơ sở dữ liệu của tôi - phòng và thiết bị. Mỗi phòng có thể có nhiều thiết bị. Tôi muốn thực hiện một listview mở rộng với tên phòng như nhóm và tên thiết bị và nhà nước như trẻ em (hoặc tắt).Android ExpandableListView và cơ sở dữ liệu SQLite

bất kỳ một thể cho tôi một ý tưởng đơn giản vì tôi là loại mới với Android và tất cả các hướng dẫn rất khó để làm cho

Đây là cơ sở dữ liệu của tôi:

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

Trả lời

13

Sau đây là một rất đơn giản, không frills thực thi danh sách mở rộng.

Bạn cần hai phương thức trong lớp trình trợ giúp db của bạn để thu thập các con trỏ bạn cần.

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

Sau đó, bạn cần phải thiết lập adapter của bạn (trong hoạt động của bạn):

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

Và cuối cùng gọi adapter và đặt nó vào danh sách của bạn (trong hoạt động của bạn):

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

Hy vọng điều này sẽ hữu ích!

EDIT

Nó nên tất cả đến với nhau như vậy:

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

Để bắt nhấp chuột, thiết lập các thính giả trong bạn hoạt động:

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ôi phải loại bỏ t getActivity() của nó startManagingCursor (childCursor) trên getChildrenCursor() vì tôi không có bất kỳ solytion nào để loại bỏ lỗi Phương thức getActivity() không được định nghĩa cho kiểu MyExpandableListAdapter !! –

+0

Ah, mã đó nằm trong một đoạn, vì vậy nó sẽ hoạt động ở đó. Bạn có thể thử sử dụng 'this' thay thế, hoặc' youractivity.this'. – Barak

+0

Tôi đã cố gắng sử dụng điều này nhưng bạn có thể thấy MyExpandableListAdapter() không phải là một hoạt động ?? !! –

Các vấn đề liên quan