如何制作两个级别的列表

时间:2013-11-20 03:47:25

标签: android android-layout android-listview

我正在创建一个简单的应用程序,我将有两个级别列表。我用ListView创建了第一级列表,但我不确定如何创建二级列表。

示例:

第一级列表是:

 Fruits
 Cookies

点击水果后,会显示以下列表:

 Bananans
 Grapes
 Apples

点击Cookie后,会显示以下列表:

 Oreo's 
 Choc Chip

第一级列表的代码:

public class MainActivity extends SherlockActivity {
    SimpleAdapter simpleAdpt;
    List<Map<String, String>> list1 = new ArrayList<Map<String,String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initList();
        ListView lv = (ListView) findViewById(R.id.listView);
        simpleAdpt = new SimpleAdapter(this, list1, android.R.layout.simple_list_item_1, new String[] {"list1"}, new int[] {android.R.id.text1});
        lv.setAdapter(simpleAdpt);
    }

    private void initList() {
        // We populate the planets
        list1.add(creatList1("list1", "Fruits"));
        list1.add(creatList1("list1", "Cookies"));
    }

    private HashMap<String, String> creatList1(String key, String name) {
        HashMap<String, String> list = new HashMap<String, String>();
        list.put(key, name);
        return list;
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

查看ExpandableListView。我适合你的需要。