android - How to filter a list view based on JSON data and filter parameters? -


i trying create listview filter in android.

we have 2 drop-downs, role , gender, , 1 search button.

when user clicks search button, want show data related selected drop down values only.

for example, if select "engineer" , "male" "role" , "gender" respectively, should show data have engineer in "role" , male in "gender".

from following json file, record should shown "bhargav".

 { "contacts": [     {             "id": "c200",             "name": "bhargav",             "email": "ab@gmail.com",             "address": "xx-xx-xxxx,x - street, x - country",             "gender" : "male",             "role" : "engineer",             "phone": {                 "mobile": "+91 0000000000",                 "home": "00 000000",                 "office": "00 000000"             }     },     {             "id": "c201",             "name": "johnny",             "email": "johnny@gmail.com",             "address": "xx-xx-xxxx,x - street, x - country",             "gender" : "female",             "role" : "charteredaccountant",             "phone": {                 "mobile": "+91 0000000000",                 "home": "00 000000",                 "office": "00 000000"             }     },     {             "id": "c202",             "name": "leonardo dicaprio",             "email": "leonardo_dicaprio@gmail.com",             "address": "xx-xx-xxxx,x - street, x - country",             "gender" : "male",             "role" : "doctor",             "phone": {                 "mobile": "+91 0000000000",                 "home": "00 000000",                 "office": "00 000000"             }      }           ]   }  

this code

public class mainactivity extends listactivity {      private progressdialog pdialog;      // url contacts json     private static string url = "http://api.examplesite.com/contacts/";      // json node names     private static final string tag_contacts = "contacts";     private static final string tag_id = "id";     private static final string tag_name = "name";     private static final string tag_email = "email";     private static final string tag_address = "address";     private static final string tag_gender = "gender";     private static final string tag_role = "role";     private static final string tag_phone = "phone";     private static final string tag_phone_mobile = "mobile";     private static final string tag_phone_home = "home";     private static final string tag_phone_office = "office";      // contacts jsonarray     jsonarray contacts = null;      // hashmap listview     arraylist<hashmap<string, string>> contactlist;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          contactlist = new arraylist<hashmap<string, string>>();          listview lv = getlistview();          // listview on item click listener         lv.setonitemclicklistener(new onitemclicklistener() {              @override             public void onitemclick(adapterview<?> parent, view view,                     int position, long id) {                 // getting values selected listitem                 string name = ((textview) view.findviewbyid(r.id.name))                         .gettext().tostring();                 string cost = ((textview) view.findviewbyid(r.id.email))                         .gettext().tostring();                 string description = ((textview) view.findviewbyid(r.id.mobile))                         .gettext().tostring();                  // starting single contact activity                 intent in = new intent(getapplicationcontext(),                         singlecontactactivity.class);                 in.putextra(tag_name, name);                 in.putextra(tag_email, cost);                 in.putextra(tag_phone_mobile, description);                 startactivity(in);              }         });          // calling async task json         new getcontacts().execute();     }      /**      * async task class json making http call      * */     private class getcontacts extends asynctask<void, void, void> {          @override         protected void onpreexecute() {             super.onpreexecute();             // showing progress dialog             pdialog = new progressdialog(mainactivity.this);             pdialog.setmessage("please wait...");             pdialog.setcancelable(false);             pdialog.show();          }          @override         protected void doinbackground(void... arg0) {             // creating service handler class instance             servicehandler sh = new servicehandler();              // making request url , getting response             string jsonstr = sh.makeservicecall(url, servicehandler.get);              log.d("response: ", "> " + jsonstr);              if (jsonstr != null) {                 try {                     jsonobject jsonobj = new jsonobject(jsonstr);                      // getting json array node                     contacts = jsonobj.getjsonarray(tag_contacts);                      // looping through contacts                     (int = 0; < contacts.length(); i++) {                         jsonobject c = contacts.getjsonobject(i);                          string id = c.getstring(tag_id);                         string name = c.getstring(tag_name);                         string email = c.getstring(tag_email);                         string address = c.getstring(tag_address);                         string gender = c.getstring(tag_gender);                         string role = c.getstring(tag_role);                          // phone node json object                         jsonobject phone = c.getjsonobject(tag_phone);                         string mobile = phone.getstring(tag_phone_mobile);                         string home = phone.getstring(tag_phone_home);                         string office = phone.getstring(tag_phone_office);                          // tmp hashmap single contact                         hashmap<string, string> contact = new hashmap<string, string>();                          // adding each child node hashmap key => value                         contact.put(tag_id, id);                         contact.put(tag_name, name);                         contact.put(tag_email, email);                         contact.put(tag_phone_mobile, mobile);                          // adding contact contact list                         contactlist.add(contact);                     }                 } catch (jsonexception e) {                     e.printstacktrace();                 }             } else {                 log.e("servicehandler", "couldn't data url");             }              return null;         }          @override         protected void onpostexecute(void result) {             super.onpostexecute(result);             // dismiss progress dialog             if (pdialog.isshowing())                 pdialog.dismiss();             /**              * updating parsed json data listview              * */             listadapter adapter = new simpleadapter(                     mainactivity.this, contactlist,                     r.layout.list_item, new string[] { tag_name, tag_email,                             tag_phone_mobile }, new int[] { r.id.name,                             r.id.email, r.id.mobile });              setlistadapter(adapter);         }      }  } 

it not working intended.

what wrong? how can implement properly?

you can set if condition in loop getting datas json , set in adapter below...

 // looping through contacts             (int = 0; < contacts.length(); i++) {                 jsonobject c = contacts.getjsonobject(i);                  if(c.getstring(tag_gender).equalsignorecase((string) role.getselecteditem()) &&  c.getstring(tag_role).equalsignorecase((string) gender.getselecteditem())) {                  string id = c.getstring(tag_id);                 string name = c.getstring(tag_name);                 string email = c.getstring(tag_email);                 string address = c.getstring(tag_address);                 string gender = c.getstring(tag_gender);                 string role = c.getstring(tag_role);                  // phone node json object                 jsonobject phone = c.getjsonobject(tag_phone);                 string mobile = phone.getstring(tag_phone_mobile);                 string home = phone.getstring(tag_phone_home);                 string office = phone.getstring(tag_phone_office);                  // tmp hashmap single contact                 hashmap<string, string> contact = new hashmap<string, string>();                  // adding each child node hashmap key => value                 contact.put(tag_id, id);                 contact.put(tag_name, name);                 contact.put(tag_email, email);                 contact.put(tag_phone_mobile, mobile);                  // adding contact contact list                 contactlist.add(contact);                  }             } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -