Reputation: 13
I write a java program that has a json parser to get some data. My problem is the comparison of values from the json parser. This is my code:
private ArrayList<HashMap<String, String>> listInfo = new ArrayList<HashMap<String, String>>();
public ArrayList<HashMap<String, String>> info() {
//Get the data
JSONObject json = JSONfunctions.getJSONfromURL(URL);
try {
//Get the elements
HashMap<Integer, String> mapValueForCompare = new HashMap<Integer, String>();
JSONArray data = json.getJSONArray("data");
//Loop the array
for (int i = 0; i < data.length(); i++) {
HashMap<String, String> mapInfo = new HashMap<String, String>();
JSONObject e = data.getJSONObject(i);
mapInfo.put("id", e.getString("id"));
mapInfo.put("title", e.getString("title"));
mapInfo.put("value", e.getString("value"));
int t = check(mapValueForCompare, mapInfo);
if (t == 1) {
mapInfo.put("isSame?", "yes");
} else {
mapInfo.put("isSame?", "no");
}
mapValueForCompare.put(i, e.getString("value"));
listInfo.add(mapInfo);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return listInfo;
}
private int check(HashMap<Integer, String> mapValueForCompare, HashMap<String, String> mapInfo) {
int result = -1;
for (int i = 0; i < mapValueForCompare.size() - 1; i++) {
if (mapValueForCompare.get(i) == mapInfo.get("value")) {
result = 1;
} else {
result = 0;
}
}
return result;
}
You see that i use a HashMap(mapValueForCompare) to collect all values and compare each value with the value of the new HashMap(mapInfo). The result is only the answer "no"! Why this? Where is my wrong? Do you have a better idea? I can't find anything else.
Upvotes: 1
Views: 1093
Reputation: 1821
Change in your check method:
if (mapInfoTest.get(i) == mapInfo.get("value"))
to
if (mapInfoTest.get(i).equals(mapInfo.get("value")))
You can simplify your code using containsKey
on your maps
Upvotes: 1