Hashmap循环方法未正确添加项目的值

时间:2019-05-27 13:31:18

标签: android graph hashmap fragment

我正在尝试编写一个在主要活动中一次运行3个片段的代码。

Fragment 1: Asks the user a question
Fragment 2: Click an image of a dog 
Fragment 3: Click a rating (using rating bar) then save result (using button) according to dog image

4个问题后,将出现一个条形图,显示每只狗的总评分。

我的问题是我的计数方法没有显示每只狗的结果,而是似乎在堆积。

例如,

Question 1... dog 1 recieved a 2
Question 2... dog 2 recieved a 4
Question 3... dog 3 recieved a 1
Question 4... dog 1 recieved a 2

条形图应该是

dog 1: 4 units high (since 2+2=4)
dog 2: 4 units high
dog 3: 1 unit high

此方法是否正确地将狗“ x”的名称与从等级栏“数字”获得的等级相关联?要使用hasmap方法执行此操作,我需要更改什么?

谢谢!

CountDogRating类

public HashMap<String, Integer> stackedBarRating(String x, int number) {

            HashMap<String, Integer> hm = new HashMap<>();
            String out = "";
            StringBuilder in = new StringBuilder();
            if (!hm.containsKey(x)) {
                hm.put(x, number);
            } else {
                hm.put(x, number + hm.get(x));
            }
            String[] arr1 = new String[hm.size()];
            String[] arr2 = new String[hm.size()];
            Set entries = hm.entrySet();
            Iterator entriesIterator = entries.iterator();
        if(4 clicks ==yes?){ // After 4 clicks, pass along info to dog class
            while (entriesIterator.hasNext()) {
                int i = 0;
                int j = 0;
                Map.Entry mapping = (Map.Entry) entriesIterator.next();
                arr1[i] = mapping.getKey().toString();//string
                arr2[i] = mapping.getValue().toString();//number
                String[] strings = new String[hm.size()];
                arr1Name = arr1[i];
                arr2Name = arr2[i];
                int arr2Number = Integer.valueOf(arr2Name);
                strings[i] = arr1Name + " " + arr2Name;
                String insert = arr1Name + " " + arr2Name;
                in.append(insert).append(System.getProperty("line.separator"));
                out = in.toString();
                dog.dataValues1SET1(Integer.valueOf(arr2Name), i); //For bargraph in dog class
                dog.labelsSet1(arr1Name);//For bargraph in dog class
                j++;
                i++;
            }
        }
        return hm;
    }
}

狗类:

public static ArrayList<String> labels1 = new ArrayList<String>();

public void dataValues1SET1(float st1, int st2) {
    dataVales1.add(new BarEntry(st1, st2));
}

public List<BarEntry> dataValues1GET1() {
    return dataVales1;
}

0 个答案:

没有答案