【问题标题】:How to iterate a ArrayList inside a HashMap Android如何在 HashMap Android 中迭代 ArrayList
【发布时间】:2015-07-10 06:02:01
【问题描述】:

我创建了一个类似这样的HashMap

hashDataChild = new HashMap<ArrayList<String>, List<String>>

我想要做的是迭代我的HashMap 中的ArrayList

基本上我想实现这样的目标

ArrayList<String> arrayOfString;

for (Integer i = 0; i<=header_data.size(); i++){
     hashDataChild.put(arrayOfString.get(i), child);
}

因此,每当它循环时,每个标题都会显示一个子项。

我正在尝试用它创建一个可扩展的列表视图。我怎样才能实现这样的目标?

是否有必要将我的HashMap 放在一个像这样的 ArrayList 中

ArrayList<HashMap<ArrayList<String>, List<String>>> 

因为我查找了一些示例并找到了此代码,但没有说明它的作用和方式。

`List<String>` for my second argument in my HashMap i created this

child = new ArrayList<String>();

    child.add("Child 1");
    child.add("Child 2");
    child.add("Child 3");

我使用ArrayList&lt;String&gt; 作为我的标题的第一个参数,它的数据来自数据库的多个列

【问题讨论】:

    标签: android arraylist hashmap


    【解决方案1】:

    你可以做这样的事情......

    import java.util.ArrayList;
     import java.util.HashMap;
       import java.util.Iterator;
       import java.util.Map;
       import java.util.Map.Entry;
    
    hashDataChild = new HashMap<ArrayList<String>, List<String>>
    
    
    Iterator<Entry<ArrayList<String>, ArrayList<String>>> it=map.entrySet().iterator();
    while(it.hasNext()){
    
     Map.Entry<ArrayList<String>, List<String>>
    pair = (Map.Entry)it.next();
    
     ArrayList<String> arraylist=pair.getKey();
    
     //iterate.... here iterate logic
    List<String> list=pair.getValue();
    }
    

    【讨论】:

    • 在迭代器中我遇到一个错误,提示“类型参数的数量错误:2,需要 1”
    • 它只显示 Error:(242, 17) error: wrong number of type arguments;需要 1
    • 出现错误。抱歉,我没有任何 IDE。
    • 这里说它无法解析符号条目,所以我尝试将其更改为 Map.Entry 并且也无法解析符号映射。 map 等价于 hashDataChild 吗?
    • 导入这些包。导入 java.util.ArrayList;导入 java.util.HashMap;导入 java.util.Iterator;导入 java.util.Map;导入 java.util.Map.Entry;
    猜你喜欢
    • 1970-01-01
    • 2016-09-08
    • 2011-12-02
    • 2011-01-08
    • 2017-11-01
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    相关资源
    最近更新 更多