如何通过游标从数据库获取可扩展列表的值

时间:2011-03-07 11:35:35

标签: android sqlite cursor expandablelistview

我想知道如何通过游标

从数据库获取可扩展列表的值
public class SmplExpandable extends ExpandableListActivity {
private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";

private ExpandableListAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
    List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
    for (int i = 0; i < 10; i++) {
        Map<String, String> curGroupMap = new HashMap<String, String>();
        groupData.add(curGroupMap);
        curGroupMap.put(NAME, "Group " + i);
        curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");

        List<Map<String, String>> children = new ArrayList<Map<String, String>>();
        for (int j = 0; j < 3; j++) {
            Map<String, String> curChildMap = new HashMap<String, String>();
            children.add(curChildMap);
            curChildMap.put(NAME, "Child " + j);
            curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
        }
        childData.add(children);
    }

    // Set up our adapter
    mAdapter = new SimpleExpandableListAdapter(
            this,
            groupData,
            android.R.layout.simple_expandable_list_item_1,
            new String[] { NAME, IS_EVEN },
            new int[] { android.R.id.text1, android.R.id.text2 },
            childData,
            android.R.layout.simple_expandable_list_item_2,
            new String[] { NAME, IS_EVEN },
            new int[] { android.R.id.text1, android.R.id.text2 }
            );
    setListAdapter(mAdapter);
}

}

2 个答案:

答案 0 :(得分:0)

为什么不使用SimpleCursorTreeAdapter:http://developer.android.com/reference/android/widget/SimpleCursorTreeAdapter.html

顺便说一句,如果有人有关于SimpleCursorTreeAdapter的好教程,请与我们分享。

答案 1 :(得分:0)

以下是来自 Android API demo ExpandableList2的示例您可以按create sample project

导入代码