1

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.

1
  • That is just a warning. You can use @suppresswarnings to avoid it if you feel uncomfortable, provided your app is not meant for multi languages Commented Jul 23, 2014 at 3:40

2 Answers 2

0

Instead of using

.toLowerCase().

change it like this

.toLowerCase(Locale.getDefault())

It will help.

and also check for Null in variables and >0 check for arrays.

Sign up to request clarification or add additional context in comments.

Comments

0

That is just a warning.

  1. You can use @SuppressWarnings to avoid it if you feel uncomfortable, provided your app is not meant for multi languages OR

  2. You can use : toLowerCase(Locale.getDefault()); to avoid this warning. Make sure to import import java.util.Locale;


EDIT

Hope below code will help you:

ArrayList<JSONObject> array = new ArrayList<JSONObject>();
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < jsonArray.length(); i++) {
    try {
     JSONObject c = jsonArray.getJSONObject(i);
    if(c.getString("category_id").equals("1")
    {
         // Adding to array. Only items with category_id = 1 will be sorted.
         array.add(jsonArray.getJSONObject(i));  // This line is important

         // All other stuff here.....
          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) {
        // TODO Auto-generated catch block
        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;
        }
    }
});

EDIT AGAIN

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);
                        } // End of if c.getstring

                        } // End of inner try
                        catch (JSONException e) {
                            e.printStackTrace();
                        } // End of inner catch
                     } // End of for loop
                } 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;
                            }
                        }
                    });

            } // End  of if jsonstr
            else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;

16 Comments

thanks, but do you know why the sorting is not working?
Its because your code is not the same as the source you are trying with. You have declared array, but not assigned any value to it anywhere in your code.
In the original post, note the line array.add(jsonArray.getJSONObject(i)); This is necessary for sorting. In your code, this line is missing.
i'm trying to do what you said, but there's an error "array cannot be resolved to a variable.
Sorry, I forget to add add try { on my code :D But the ask me to add "finally" to complete blockstatements.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.