3

i am getting the data in json form like

{"Users":[
{"category_id":"1","user_email":"a@a.com"},
{"category_id":"5","user_email":"a@b.com"},
{"category_id":"1","user_email":"a@c.com"},
{"category_id":"5","user_email":"a@d.com"},
{"category_id":"3","user_email":"a@d.com"}]}

now my question is can it is possible in android to filter this json base on category_id for example if i am select 1 from my spinner then it should be return the data which is content the category_id = 1 example

{"Users":[{"category_id":"1","user_email":"a@a.com"},
{"category_id":"1","user_email":"a@c.com"}]}

if select 3 then it should return the user which is content the category_id = 3

{"Users":[
{"category_id":"3","user_email":"a@d.com"}]}

i just want to know is there any method is available in android so i can easily filter the data.

Sagar Devani
  • 242
  • 1
  • 3
  • 10
  • you need to for each loop in the users array and when the yourLoop.category_id == 3 do what ever you want and then exit loop – Mina Gabriel Aug 11 '14 at 18:07

3 Answers3

4

First you need to create your variables in your activity

//URL to get JSON
private static String url = "your url to parse data";

//JSON Node Names
private static final String TAG_USERS = "users";
private static final String TAG_CATEGORY_ID = "category_id";
private static final String TAG_USER_EMAIL = "user_email";

// Data JSONArray
JSONArray users = null;

//HashMap to keep your data
ArrayList<HashMap<String, String>> userList;

inside onCreate() function instantiate your userList

userList = new ArrayList<HashMap<String, String)>>();

then you need to parse your data and populate this ArrayList in doInBackground() function

//Create service handler class instance
ServiceHandler sh = new ServiceHandler();

//Url request and response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);

if(jsonStr != null) {
    try {
        JSONObject jsonObj = new JSONObject(jsonStr);

        //Get Json Array Node
        users = jsonObj.getJSONArray(TAG_USERS);

        // Looping through all data
        for(int i = 0; i < users.length(); i++) {
            JSONObject u = users.getJSONObject(i);

            String category_id = u.getString(TAG_CATEGORY_ID);
            String user_email = u.getString(TAG_USER_EMAIL);

            // Temporary HashMap for single data
            HashMap<String, String> user = new HashMap<String, String>();

            // Adding each child node to Hashmap key -> value
            user.put(TAG_CATEGORY_ID, category_id);
            user.put(TAG_USER_EMAIL, user_email);

            //Adding user to userList
            userList.add(user);
        }
    }catch (JSONException e) {
        e.printStackTrace();
    }
} else {
    Log.e("ServiceHandler", "Couldn't get any data from url");
}

Now you have your data stored in your userList. You can use your userList however you want. To get the category_id field in the list you can use this syntax

// i being the counter of your loop
String catId = userList.get(i).get("category_id");
if(catId == 3) {
    ... your code
}
anetha
  • 118
  • 2
  • 6
  • i am trying to avoid a looping at the filter time because there are around 5000 user data and set the user in list and when the spinner value is selected then quick new data must be set so is it possible using **Predicates** in android. – Sagar Devani Aug 12 '14 at 03:50
1

finally i got the answer..

private void applySearch(String searchStr) {
            ArrayList<User> searchEmail = new ArrayList<User>();


            for (int i = 0; i < UserArray.size(); i++) {
                if(UserArray.get(i).getcategory_id().toLowerCase().contains(searchStr.toLowerCase())) {
                    searchEmail.add(UserArray.get(i));
                }
            }

           // set the adapter hear 

        }

without for loop it is not possible ... UserArray is my array list content user object

Sagar Devani
  • 242
  • 1
  • 3
  • 10
0

Use JSONObject from the Android SDK. Check out the documentation here:

http://developer.android.com/reference/org/json/JSONObject.html

You basically need to parse the JSON, then loop through and only operate on the items with the specified category id.

For larger data sets, using JsonReader will be more performant if written properly.

http://developer.android.com/reference/android/util/JsonReader.html

SoWeLie
  • 1,381
  • 12
  • 16
  • hi @SoWeLie i know that it is possible through set all object in ArrayList and then set for loop for filter the data but is there any other way which is give fast execution because my data is content more then 5000 user data if there any other method or any 3rd party lib is available which is give fast execution and give result ... thank you. – Sagar Devani Aug 11 '14 at 18:05
  • 1
    If you're dealing with 5,000 rows, you're going to want to filter the data on the server side. Parsing that much JSON on the client is not a good idea. If server side filtering is not possible, using JsonReader should be faster than JSONObject. http://developer.android.com/reference/android/util/JsonReader.html – SoWeLie Aug 11 '14 at 18:09