Android Api Demos登顶之路(109)View-->ExpanableLists-->Simple Adapter

/*这个demo演示了为ExpanableListView提供数据的第三种方式使用SimpleExpandableListAdapter*/
public class MainActivity extends ExpandableListActivity {
    private static final String NAME = "name";
    private static final String IS_EVEN = "is_even";

    private ExpandableListAdapter mAdapter;

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

        // 定义group的数据集合,Map集合中存放了需要在group当中显示的数据的名称和值
        List<Map<String, String>> groupDatas = new ArrayList<Map<String, String>>();
        // 定义child的数据集合,第一个list表示分属于哪一个group,第二个list表示哪个child
        List<List<Map<String, String>>> childDatas = new ArrayList<List<Map<String, String>>>();

        // 为数据集填充数据
        for (int i = 0; i < 20; i++) {
            Map<String, String> curGroupData = new HashMap<String, String>();
            curGroupData.put(NAME, "Group " + i);
            curGroupData.put(IS_EVEN, (i % 2 == 0) ? "This group is even"
                    : "This group is odd");
            groupDatas.add(curGroupData);

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

        mAdapter = new SimpleExpandableListAdapter(
                this,
                groupDatas,
                // 伸展状态android.R.layout.simple_expandable_list_item_1中只有一个textView只能显示一项
                android.R.layout.simple_expandable_list_item_2,
                // 收缩状态使用android.R.layout.simple_expandable_list_item_2,这个布局中包含两个textView
                android.R.layout.simple_expandable_list_item_1,
                new String[]{NAME,IS_EVEN},
                new int[]{android.R.id.text1,android.R.id.text2}, 
                childDatas, 
                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);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值