java从HashMap<String,ArrayList<String>>
[更新代码](抱歉,伙计们,我没有提供全部代码,因为根据我的经验,大型代码似乎“吓跑”了可能的帮助者。)
对于ExpandableListView,我想构建一个HashMap<String, ArrayList<String>>
,其中String
是一个类别的名称ArrayList<String>
是属于该类别的动物的名称。我按如下方式填充HashMap:
HashMap<String, ArrayList<String>> map_groups_childs;
ArrayList<String> list_group_titles;
private void prepareListData(ArrayList<Id_triv_cat> search_results) {
list_group_titles = new ArrayList<String>(); // this is a list of group titles
map_groups_childs = new HashMap<String, ArrayList<String>>(); // this is a map. each group title gets a list of its respective childs
// temporary List of items for a certain category/group
ArrayList<String> temp_childs_list = new ArrayList<String>();
// "search_results" is an ArrayList of self defined objects each containing an ID, a name and a category name
int count = search_results.size();
int i_cat = 0;
int i=0;
// if category "i" is the same as the next category "i+1", add child to list
for (i=0; i<count-1; i++) {
// build group with category name
list_group_titles.add(search_results.get(i).get_type());
// while category does not change, add child to the temporary childs-array
while (i<=count && search_results.get(i).get_type().equals(search_results.get(i+1).get_type())) {
temp_childs_list.add(search_results.get(i).get_name());
i++;
}
// must be done, as the while loop does not get to the last "i" of every category
temp_childs_list.add(search_results.get(i).get_name());
Log.i("DEBUG", temp_childs_list.size()); // --> returns always more than 0
Log.i("DEBUG", temp_childs_list.toString()); // --> returns always something like [word1, word2, word3, ...]
Log.i("DEBUG", list_group_titles.get(i_cat)); // --> returns always a single word like "Insekten"
// add [group_title:list_of_childs] to map
map_groups_childs.put(list_group_titles.get(i_cat++), temp_childs_list);
// clear temp_list, otherwise former category's species will be added to new category
temp_childs_list.clear();
}
Log.i("DEBUG", map_groups_childs.containsKey("Insekten")); // --> returns true
Log.i("DEBUG", map_groups_childs.size()); // --> returns 10
Log.i("DEBUG", map_groups_childs.get("Insekten").size()); // --> returns 0
Log.i("DEBUG", map_groups_childs.toString()); // --> returns {Insekten=[], Reptilien=[], ...}
}
在for-and-while循环中使用相同的i
可能看起来是错误的或令人困惑的,但这是可以的。不以任何方式跳过i
或使用两次
我放在HashMap中的所有键都在那里,但是我想要获得的ArrayList(例如)map_groups_childs.get("Insekten")
是空的。我做错了什么
# 1 楼答案
即使进行了编辑,您的代码仍然缺少关于正在发生的事情的大量线索。我做了一些猜测,如果我是对的,你这样做太难了
我推断了一个类
Id_triv_cat
的存在,它的getter名为get_name()
和get_type()
我还编写了这段代码来测试
prepareListData()
方法虽然我可以像一个优秀的SE居民一样修复你的小缺陷,但我还是要冒险将所有这些迁移到程序员身上,因为你最大的问题是设计问题。您的代码缺少清晰的本地标识符。你不是在做本地人,而是在用java实用程序类举办一场点滴狂欢节,这让你的代码毫无意义地难以理解
如果,正如我所怀疑的,您正试图从
list
的Id_triv_cat
中使用其类型作为键、名称作为值来构建映射,那么请尝试以下操作:这将显示:
my_map_groups_childs = {Insekten=[insekten name1, insekten name2], type1=[name1], type3=[name3], type2=[name2]}
看看几个精心挑选的当地人怎么能让生活变得如此轻松
如果这不是你想要的,你就必须让你的问题更清楚
Radiodef是对的。在用Java编写代码时,确实应该使用camelCase
# 2 楼答案
对象在Java中作为引用传递。你总是把相同的列表放到地图上,并在每次迭代后清除它。因此,映射中的每个值都指向同一个空列表
你可能需要这样的东西:
因此,每次迭代都会创建一个新列表
我也同意@CandiedOrange,你的代码很混乱,可能过于复杂。一般来说,像List和Map这样的抽象概念的要点是不通过一直计算数字索引来访问对象
请注意,在Java中,惯例是变量的标识符是camelCase,而不是不得分的
# 3 楼答案
我几乎能告诉你你需要的是下面第二组中的5行