1

I'm trying to implement an expandable listview with data from a remote server. I've already have the JSON part covered. My sample has three value sets returned (confirmed by checking logcat on the original JSON response). My problem now is while dividing the JSON return into header and child datas, the first value set is skipped. My code is as follows:

int lisDataHeaderCounter = 0;
String searchKey;

for (int i = 0; i < components.length(); i++) {                     
    List<String> component_value = new ArrayList<String>();

    searchKey = main_components.get(i);
    if (!listDataHeader.contains(searchKey)) {
        listDataHeader.add(searchKey);

        Iterator<Map.Entry<String, String>> entries = sub_components.entrySet().iterator(); 

        while (entries.hasNext()) {
            Map.Entry<String, String> entry = entries.next();

            Log.d("getValue() ", "+ " + entry.getValue());
            if (searchKey == entry.getKey())
                component_value.add(entry.getValue());
        }

        listDataChild.put(listDataHeader.get(lisDataHeaderCounter), component_value);
        lisDataHeaderCounter++;
    }                       
}

I've also tried the code below and it still has the same result.

for (Map.Entry<String, String> entry : sub_components.entrySet()) {
    if (searchKey == entry.getKey())
        component_value.add(entry.getValue());
}

Here is a sample of the JSON response that is being process by the above codes:

[{"activity_code":"1","activity_name":"Midterm Exam"},
 {"activity_code":"1","activity_name":"Final Exam"},
 {"activity_code":"2","activity_name":"Project"}]

With the current codes, in the for loop, the first value of searchKey is '1'. When I placed a Log.d(); in the while loop to check what the first value is read, I found that it is "Final Exam" and not "Midterm Exam". Is there a way for me to get the value of the first data set before it goes into the while loop?

Here is a workaround I've made to ensure that the first value would be included to the sub_components. But I guess it doesn't look neat. If anyone has a better solution, please feel free to share.

for (int i = 0; i < components.length(); i++) {
    JSONObject c = components.getJSONObject(i);

    String formula_code = c.getString(TAG_FORMULA_CODE);
    String component_name = c.getString(TAG_ACTIVITY_NAME);

    main_components.add(formula_code);
    sub_components.put(formula_code, component_name);

    if (!listDataHeader.contains(formula_code))
        listDataHeader.add(formula_code);
    if (i == 0) {
        component_value.add(component_name);
    }
}

for (int i = 0; i < listDataHeader.size(); i++) {
    for (Map.Entry<String, String> entry : sub_components.entrySet()) {
        if (listDataHeader.get(i) == entry.getKey())
            component_value.add(entry.getValue());
    }

    listDataChild.put(listDataHeader.get(i), component_value);
    component_value = new ArrayList<String>();
}
  • have you tried using a do while loop? That will always calculate the first value before beginning iteration. – sirFunkenstine Sep 26 '13 at 16:59
  • By skipped, do you simply mean that the `then` part of your `if` isn't getting executed? Since you're comparing `String`s with `==` (`searchKey` is a string), the test may fail even when the strings have the same content. See [How do I compare strings in Java?](http://stackoverflow.com/q/513832/1281433). – Joshua Taylor Sep 26 '13 at 18:59
  • *sirFunkenstine, I don't know how to implement the iteration of a Hashmap using do-while loop. Could you please give a sample? *Joshua Taylor, if you compare it to a 2D-array, the for-loop begins to read the data on [0][0] up to [n][0]. The while loop, which should read [0][1] first, goes to [1][1]. Still can't understand why [0][1] is skipped. – user2794335 Sep 26 '13 at 21:03
  • Why do you need to iterate over HashMap when you can just use `component_value.add(sub_components.get(searchKey))`? What is `sub_components`? The first entry in the JSON? It can't be the whole JSON looking from the structure (a map from `String` to `String`). I'm guessing that your mistake may lie somewhere in the fact that the first and second JSON line has the same `activity_code`. – justhalf Sep 27 '13 at 05:49
  • From the JSON response, I'm trying to separate the data needed for the expandable listview with the activity_code being the header while the activity name the child. Hopefully, it would result to header "1" having to children ("Midterm Exam" and "Final Exam") while header "2" only has "Project" as child. – user2794335 Sep 27 '13 at 06:51
  • By iterating over the HashMap, I want to filter the activity_names with the same activity_codes then add them as the children of the said activity_code. – user2794335 Sep 27 '13 at 06:58

1 Answers1

0

I can't see any off by one error on the two, but perhaps it's the searchKey == entry.getKey(), that might have to be searchKey.equals(entry.getKey()), but I would need to see more code to know for sure.

pbae
  • 3
  • 3
  • I tried the searchKey.equals(entry.getKey()) but it has the same result. The codes I've provided are the ones handling the separation of the data from the JSON response for the header and child. So once the JSON response is received, these are what handles them next. The rest of the codes are just pretty standard ones I got from the tutorials I've been using. – user2794335 Sep 26 '13 at 21:12