我使用SimpleExpandableListAdapter为我的应用程序创建ExpandableListView。我想知道如何更好地使用列表和地图,以及它们在实践中是什么。

 //collection for elements of a single group;
ArrayList<Map<String, String>> childDataItem;

//general collection for collections of elements
ArrayList<ArrayList<Map<String, String>>> childData;


Map<String, String> m;

我知道如何遍历地图的数组列表,这对我来说不是问题,但我被卡住了。
childData = new ArrayList<>();
    childDataItem = new ArrayList<>();
    for (String phone : phonesHTC) {
        m = new HashMap<>();
        m.put("phoneName", phone);
        childDataItem.add(m);
    }
    childData.add(childDataItem);

    childDataItem = new ArrayList<>();
    for (String phone : phonesSams) {
        m = new HashMap<String, String>();
        m.put("phoneName", phone);
        childDataItem.add(m);
    }
    childData.add(childDataItem);

    // создаем коллекцию элементов для третьей группы
    childDataItem = new ArrayList<>();
    for (String phone : phonesLG) {
        m = new HashMap<String, String>();
        m.put("phoneName", phone);
        childDataItem.add(m);
    }
    childData.add(childDataItem);

我想记录childdata包含的内容(<ArrayList<Map<String, String>>),但我不确定我是否做对了。(第二个循环是映射迭代的简单数组列表)
    for (ArrayList<Map<String, String>> outerEntry : childData) {
       for(Map<String, String> i:outerEntry ) {
           for (String key1 : i.keySet()) {
               String value1 = i.get(key1);
               Log.d("MyLogs", "(childData)value1 = " + value1);
               Log.d("MyLogs", "(childData)key = " + key1);
           }
         }


        for (Map<String, String> innerEntry : childDataItem) {
            for (String key : innerEntry.keySet()) {
                String value = innerEntry.get(key);
                Log.d("MyLogs", "(childDataItem)key = " + key);
                Log.d("MyLogs", "(childDataItem)value = " + value);
            }
        }
    }

最佳答案

如果要记录childdata的所有元素,则不需要最后一个循环,您已经在第一个循环中获取了它们。请从程序中删除以下代码,它将记录所有childdata项。

for (Map<String, String> innerEntry : childDataItem) {
    for (String key : innerEntry.keySet()) {
        String value = innerEntry.get(key);
        Log.d("MyLogs", "(childDataItem)key = " + key);
        Log.d("MyLogs", "(childDataItem)value = " + value);
    }
}

上面的循环正在对childdataitem进行迭代,并且您在代码中反复使用同一引用,因此在本例中,上面的循环将只包含最新的映射项。
为了简单起见,我将log语句改为sysout,下面是示例和输出:
    ArrayList<Map<String, String>> childDataItem;
    //general collection for collections of elements
    ArrayList<ArrayList<Map<String, String>>> childData;

    Map<String, String> m;


    childData = new ArrayList<>();
    childDataItem = new ArrayList<>();
        m = new HashMap<>();
        m.put("phoneName", "HTC");
        m.put("phoneName1", "HTC1");
        childDataItem.add(m);
    childData.add(childDataItem);

    childDataItem = new ArrayList<>();
        m = new HashMap<String, String>();
        m.put("phoneName", "Samsung");
        childDataItem.add(m);
    childData.add(childDataItem);

    // создаем коллекцию элементов для третьей группы
    childDataItem = new ArrayList<>();
        m = new HashMap<String, String>();
        m.put("phoneName", "LG");
        childDataItem.add(m);
    childData.add(childDataItem);


    for (ArrayList<Map<String, String>> outerEntry : childData) {
       for(Map<String, String> i:outerEntry ) {
           for (String key1 : i.keySet()) {
               String value1 = i.get(key1);
               System.out.println("MyLogs (childData)value1 = " + value1);
               System.out.println("MyLogs (childData)key = " + key1);
           }
         }
    }

输出
MyLogs (childData)value1 = HTC1
MyLogs (childData)key = phoneName1
MyLogs (childData)value1 = HTC
MyLogs (childData)key = phoneName
MyLogs (childData)value1 = Samsung
MyLogs (childData)key = phoneName
MyLogs (childData)value1 = LG
MyLogs (childData)key = phoneName

10-05 21:39