i am getting the data in json form like
{"Users":[
{"category_id":"1","user_email":"[email protected]"},
{"category_id":"5","user_email":"[email protected]"},
{"category_id":"1","user_email":"[email protected]"},
{"category_id":"5","user_email":"[email protected]"},
{"category_id":"3","user_email":"[email protected]"}]}
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":"[email protected]"},
{"category_id":"1","user_email":"[email protected]"}]}
if select 3 then it should return the user which is content the category_id = 3
{"Users":[
{"category_id":"3","user_email":"[email protected]"}]}
i just want to know is there any method is available in android so i can easily filter the data.
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
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With