android - How to filter list view using search edittext with JSON url -
am using edittext search form json
url datas, give search field. search each letter type on edittext
filter listview
, @ same time listview
reload, if edittext
empty show current list. how please me...
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.electronic_main); search = (edittext) findviewbyid(r.id.text_search); listview = (listview) findviewbyid(r.id.listview); search.addtextchangedlistener(new textwatcher() { @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void ontextchanged(charsequence s, int start, int before, int count) { arraylist<hashmap<string, string>> arraytemplist = new arraylist<hashmap<string, string>>(); string searchstring = search.gettext().tostring().tolowercase(locale.getdefault()); listview.settextfilterenabled(true); (int = 0; < arraylist.size(); i++) { string currentstring = arraylist.get(i).get(electronic.workername); string currentplacestring = arraylist.get(i).get(electronic.location); string currentshopstring = arraylist.get(i).get(electronic.shopname); if (searchstring.equalsignorecase(currentstring) || searchstring.equalsignorecase(currentshopstring) || searchstring.equalsignorecase(currentplacestring)) { arraytemplist.add(arraylist.get(i)); } } adapter = new electronicadapter(electronic.this, arraytemplist); listview.setadapter(adapter); adapter.notifydatasetchanged(); } @override public void aftertextchanged(editable s) { adapter.notifydatasetchanged(); } }); boolean isinternetpresent = cd.isconnectingtointernet(); // true or false if (isinternetpresent) { new downloadjson().execute(); } else { intent lost = new intent(electronic.this, netconnection.class); startactivity(lost); } } private class downloadjson extends asynctask<void, void, boolean> { @override protected void onpreexecute() { super.onpreexecute(); mprogressdialog = new progressdialog(electronic.this); mprogressdialog.setmessage("loading..."); mprogressdialog.setindeterminate(false); mprogressdialog.show(); } @override protected boolean doinbackground(void... params) { arraylist = new arraylist<hashmap<string, string>>(); jsonobject = jsonfunctions .getjsonfromurl("http://beta.json-generator.com/api/json/get/d0_dgqt"); try { jsonarray = jsonobject.getjsonarray("eletronics"); (int = 0; < jsonarray.length(); i++) { hashmap<string, string> map = new hashmap<string, string>(); jsonobject = jsonarray.getjsonobject(i); map.put("workername", jsonobject.getstring("workername")); map.put("shopname", jsonobject.getstring("shopname")); map.put("experience", jsonobject.getstring("experience")); map.put("fare", jsonobject.getstring("fare")); map.put("location", jsonobject.getstring("location")); map.put("rating", jsonobject.getstring("rating")); map.put("address", jsonobject.getstring("address")); map.put("workerimage", jsonobject.getstring("workerimage")); map.put("shopphoto", jsonobject.getstring("shopphoto")); map.put("cell", jsonobject.getstring("cell")); arraylist.add(map); } } catch (jsonexception e) { e.printstacktrace(); } return true; } @override protected void onpostexecute(boolean arg) { listview = (listview) findviewbyid(r.id.listview); adapter = new electronicadapter(electronic.this, arraylist); listview.setadapter(adapter); mprogressdialog.dismiss(); } } }
search=(edittext)findviewbyid(r.id.serach); listview=(listview)findviewbyid(r.id.listview); listview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { // todo auto-generated method stub string gh = listview.gettextfilter().tostring(); text.settext(gh); } }); listview.settextfilterenabled(true); search.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence arg0, int arg1, int arg2, int arg3) { // todo auto-generated method stub string searchstring = search.gettext().tostring().tolowercase(locale.getdefault()); log.d("@w2w2w2w",""+searchstring); int realtext=searchstring.length(); arraytemplist.clear(); (int = 0; < arraylist.size(); i++) { string currentstring = arraylist.get(i).get("name").tostring(); string currentplacestring = arraylist.get(i).get("city").tostring(); string currentshopstring = arraylist.get(i).get("landtype").tostring(); if(realtext<=currentstring.length()&&realtext<=currentplacestring.length()&&realtext<=currentshopstring.length()) { if (searchstring.equalsignorecase(currentstring.substring(0,realtext)) || searchstring.equalsignorecase(currentshopstring.substring(0,realtext)) || searchstring.equalsignorecase(currentplacestring.substring(0,realtext))) { arraytemplist.add(arraylist.get(i)); adapter = new listviewadapter(edit.this, arraytemplist); listview.setadapter(adapter); } } else{ adapter.notifydatasetchanged(); } }} @override public void beforetextchanged(charsequence arg0, int arg1, int arg2, int arg3) { // todo auto-generated method stub } @override public void aftertextchanged(editable arg0) { // todo auto-generated method stub } }); // execute downloadjson asynctask }
Comments
Post a Comment