Hashmap列表更新所有字符串值

时间:2017-05-03 10:26:33

标签: java android hashmap expandablelistview

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

    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();
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

当你使用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++;
                }
            }
        }