gpt4 book ai didi

java - 哈希表列表更新所有字符串值

转载 作者:行者123 更新时间:2023-11-30 00:33:52 26 4
gpt4 key购买 nike

我正在使用可扩展 ListView 并需要标题列表和每个标题的子项...我在下面的代码中面临的问题是它正在更新所有字符串/键值而不是目标值...

    private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
databaseProjects = new ArrayList<>();
arrayListTagNames=new ArrayList<String>();
arrayListTagNames.clear();
int countheaders = 0;


for(int i = 0; i < databasePositions.size(); i++) {


if (!(databasePositions.get(i).equals("None"))) {

listDataHeader.add(databasePositions.get(i));

databaseProjects = databaseAdapter.getAllProjectsNameOnlyDBusingserverID(databasePositionsid.get(i));

for (int j = 0; j < databaseProjects.size(); j++) {
if (!(databaseProjects.get(j).equals("None"))) {
arrayListTagNames.add( databaseProjects.get(j));
}
}

listDataChild.put(listDataHeader.get(countheaders), arrayListTagNames);


countheaders++;
arrayListTagNames.clear();
}
}
}

最佳答案

当您使用 arrayListTagNames.clear() 清除 arrayList 时,它会影响放入 hashmap 的内容,而不是清除创建一个新的数组列表并放入 HashMap,尝试这样的事情

private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
databaseProjects = new ArrayList<>();
int countheaders = 0;


for(int i = 0; i < databasePositions.size(); i++) {


if (!(databasePositions.get(i).equals("None"))) {
arrayListTagNames=new ArrayList<String>();

listDataHeader.add(databasePositions.get(i));

databaseProjects = databaseAdapter.getAllProjectsNameOnlyDBusingserverID(databasePositionsid.get(i));

for (int j = 0; j < databaseProjects.size(); j++) {
if (!(databaseProjects.get(j).equals("None"))) {
arrayListTagNames.add( databaseProjects.get(j));
}
}

listDataChild.put(listDataHeader.get(countheaders), arrayListTagNames);


countheaders++;
}
}
}

关于java - 哈希表列表更新所有字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43757446/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com