I am new. I'm trying to sorting the data that I get from json parse,but nothing work. Not even an error. I try How to Sort JSON Array from JSON Objects in Android?
I don't know what's wrong with my code, because there's no error. But there's a warning "Implicitly using the default locale is common source of bugs: Use toLowerCase(Locale) instead" on here
return (lhs.getString("item_name").toLowerCase().compareTo(rhs.getString("item_name").toLowerCase()));
Is there anyone can help me to solve this problem?
Here's my java code
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
ArrayList<JSONObject> array = new ArrayList<JSONObject>();
JSONArray jsonArray = new JSONArray();
// looping through All Contacts
for (int i = 0; i < foods.length(); i++) {
try {
JSONObject c = foods.getJSONObject(i);
if(c.getString("category_id").equals("1")) {
// Adding to array. Only items with category_id = 1 will be sorted.
array.add(foods.getJSONObject(i)); // This line is important
String item_id = c.getString(TAG_ITEM_ID);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_ITEM_ID, item_id);
// adding contact to contact list
foodslist.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Collections.sort(array, new Comparator<JSONObject>() {
@Override
public int compare(JSONObject lhs, JSONObject rhs) {
// TODO Auto-generated method stub
try {
return (lhs.getString("Name").toLowerCase(Locale.getDefault()).compareTo(rhs.getString("Name").toLowerCase(Locale.getDefault())));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
}
});
}
}
else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
And here's my Json data
[{"id":"201","category_id":"0","category_name":null,"item_name":"","description":"","address":"","area":""}
Thanks before.