Home | History | Annotate | Download | only in view
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.apis.view;
     18 
     19 import android.app.ExpandableListActivity;
     20 import android.os.Bundle;
     21 import android.widget.ExpandableListAdapter;
     22 import android.widget.SimpleExpandableListAdapter;
     23 
     24 import java.util.ArrayList;
     25 import java.util.HashMap;
     26 import java.util.List;
     27 import java.util.Map;
     28 
     29 
     30 /**
     31  * Demonstrates expandable lists backed by a Simple Map-based adapter
     32  */
     33 public class ExpandableList3 extends ExpandableListActivity {
     34     private static final String NAME = "NAME";
     35     private static final String IS_EVEN = "IS_EVEN";
     36 
     37     private ExpandableListAdapter mAdapter;
     38 
     39     @Override
     40     public void onCreate(Bundle savedInstanceState) {
     41         super.onCreate(savedInstanceState);
     42 
     43         List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
     44         List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
     45         for (int i = 0; i < 20; i++) {
     46             Map<String, String> curGroupMap = new HashMap<String, String>();
     47             groupData.add(curGroupMap);
     48             curGroupMap.put(NAME, "Group " + i);
     49             curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
     50 
     51             List<Map<String, String>> children = new ArrayList<Map<String, String>>();
     52             for (int j = 0; j < 15; j++) {
     53                 Map<String, String> curChildMap = new HashMap<String, String>();
     54                 children.add(curChildMap);
     55                 curChildMap.put(NAME, "Child " + j);
     56                 curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
     57             }
     58             childData.add(children);
     59         }
     60 
     61         // Set up our adapter
     62         mAdapter = new SimpleExpandableListAdapter(
     63                 this,
     64                 groupData,
     65                 android.R.layout.simple_expandable_list_item_1,
     66                 new String[] { NAME, IS_EVEN },
     67                 new int[] { android.R.id.text1, android.R.id.text2 },
     68                 childData,
     69                 android.R.layout.simple_expandable_list_item_2,
     70                 new String[] { NAME, IS_EVEN },
     71                 new int[] { android.R.id.text1, android.R.id.text2 }
     72                 );
     73         setListAdapter(mAdapter);
     74     }
     75 
     76 }
     77