はありません、非常に簡単です拡張可能なリストの実装は不要です。
必要なカーソルを収集するには、dbヘルパークラスに2つのメソッドが必要です。
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.
}
}
希望します。
EDIT
それがすべてのようにのように一緒に来る必要があります:
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;
}
});
:
EDIT 2
は、クリックをキャッチするには、あなたの活動をリスナーを設定します私はtを取り除かなければならないgetActivity()。startManagingCursor(childCursor)(getChildrenCursor()はエラーを取り除くソリテイがないため)getActivity()メソッドはMyExpandableListAdapterタイプでは未定義です!! –
ああ、そのコードは断片にあったので、そこではうまくいくはずです。代わりに 'this'を使うか、' youractivity.this'を試してみてください。 – Barak
これを使用しようとしましたが、MyExpandableListAdapter()がアクティビティではありません。 –