search - Android Listview searching is not working properly -
kindly me in code, doesn't search perfect other values remain on position except search able string.
here code of filterable class
import java.util.arraylist; import java.util.arrays; import java.util.list; import java.util.stringtokenizer; import android.app.activity; import android.content.intent; import android.graphics.interpolator.result; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.button; import android.widget.filter; import android.widget.filterable; import android.widget.textview; public class listviewadapter_claimlist extends baseadapter implements filterable{ activity context; string claimid[]; string ref[]; string policy[]; string natureofloss[]; string registration[]; string status[]; private itemfilter mfilter = new itemfilter(); private list<string>originaldata = null; private list<string>filtereddata = null; private list<string>claimidfilter = null; private list<string>reffilter = null; private list<string>nautreoflossfilter = null; private list<string>registrationfilter = null; private list<string>statusfilter = null; arraylist<abc> alldata = new arraylist<abc>(); public listviewadapter_claimlist(activity context, string[] claimid, string[] ref,string[] policy, string[] natureofloss,string[] registration, string[] status) { super(); this.context = context; this.claimid = claimid; this.ref = ref; this.policy = policy; this.natureofloss = natureofloss; this.registration = registration; this.status = status; this.filtereddata = new arraylist<string>(arrays.aslist(this.policy)); this.originaldata = new arraylist<string>(arrays.aslist(this.policy)); this.claimidfilter = new arraylist<string>(arrays.aslist(this.claimid)); this.reffilter = new arraylist<string>(arrays.aslist(this.ref)); this.nautreoflossfilter = new arraylist<string>(arrays.aslist(this.natureofloss)); this.registrationfilter = new arraylist<string>(arrays.aslist(this.registration)); this.statusfilter = new arraylist<string>(arrays.aslist(this.status)); for(int = 0; i<this.claimid.length; i++) { abc = new abc(); a.setclaimid(claimid[i]); a.setref(ref[i]); a.setpolicy(policy[i]); a.setnatureofloss(natureofloss[i]); a.setregistration(registration[i]); a.setstatus(status[i]); alldata.add(a); } } public int getcount() { return filtereddata.size(); } public object getitem(int position) { return filtereddata.get(position); } public long getitemid(int position) { return 0; } private class viewholder { textview cliamid; textview ref; textview policy; textview natureofloss; textview registration; textview status; } public view getview(int position, view convertview, final viewgroup parent) { final viewholder holder; layoutinflater inflater = context.getlayoutinflater(); if (convertview == null) { convertview = inflater.inflate(r.layout.rowitem_cliamlist, null); holder = new viewholder(); holder.cliamid = (textview) convertview.findviewbyid(r.id.tv_claims); holder.ref = (textview) convertview.findviewbyid(r.id.tv_referencepolicy); holder.policy = (textview) convertview.findviewbyid(r.id.tv_policyno); holder.natureofloss = (textview) convertview.findviewbyid(r.id.tv_natureoflos); holder.registration = (textview) convertview.findviewbyid(r.id.tv_registration); holder.status = (textview) convertview.findviewbyid(r.id.tv_claimstatus); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } abc aa = alldata.get(position); holder.cliamid.settext(aa.getclaimid()); holder.ref.settext(aa.getref()); holder.policy.settext(filtereddata.get(position)); holder.natureofloss.settext(aa.getnatureofloss()); holder.registration.settext(aa.getregistration()); holder.status.settext(aa.getstatus()); return convertview; } //-------------------------------------------------------------------------------- public filter getfilter() { return mfilter; } private class itemfilter extends filter { @override protected filterresults performfiltering(charsequence constraint) { string filterstring = constraint.tostring().tolowercase(); filterresults results = new filterresults(); final list<string> list = originaldata; int count = list.size(); final arraylist<string> nlist = new arraylist<string>(count); string filterablestring ; (int = 0; < count; i++) { filterablestring = list.get(i); if (filterablestring.tolowercase().contains(filterstring)) { nlist.add(filterablestring); } } results.values = nlist; results.count = nlist.size(); return results; } @suppresswarnings("unchecked") @override protected void publishresults(charsequence constraint, filterresults results) { filtereddata = (arraylist<string>) results.values; notifydatasetchanged(); } } public class abc { string claimid; string ref; string policy; string natureofloss; string registration; string status; public void setclaimid(string claimid) { this.claimid = claimid; } public void setref(string ref) { this.ref = ref; } public void setpolicy(string policy) { this.policy = policy; } public void setnatureofloss(string natureofloss) { this.natureofloss = natureofloss; } public void setregistration(string registration) { this.registration = registration; } public void setstatus(string status) { this.status = status; } public string getclaimid() { string claimid = this.claimid; return claimid; } public string getref() { string ref = this.ref; return ref; } public string getpolicy() { string policy = this.policy; return policy; } public string getnatureofloss() { string natureofloss = this.natureofloss; return natureofloss; } public string getregistration() { string registration = this.registration; return registration; } public string getstatus() { string status = this.status; return status; } } }
edit text event handler
searchtext.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence s, int start, int before, int count) { // todo auto-generated method stub claimlist.this.lviewadapter.getfilter().filter(s.tostring()); } @override public void beforetextchanged(charsequence s, int start, int count, int after) { // todo auto-generated method stub } @override public void aftertextchanged(editable s) { // todo auto-generated method stub } });
before search
when search police no(policy# in code)
thank in advance :)
you can choose trick this, set textwatcher on edit text entering search text.
searchtext.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence s, int start, int before, int count) { } @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void aftertextchanged(editable s) { string text = searchtext.gettext().tostring() .tolowercase(locale.getdefault()); lviewadapter.filter(text); } });
put filter method in adapter class
public void filter(string chartext) { chartext = chartext.tolowercase(locale.getdefault()); temp.clear(); if (chartext.length() == 0) { temp.addall(list_original); } else { (abc abc: list_original) { // if want search via policy if (abc.getpolicy().tolowercase(locale.getdefault()) .contains(chartext)) { temp.add(abc); } } } notifydatasetchanged(); }
and create 2 abc type arraylists 1 temp , 1 original, pass data listview in getview method temp list, can make changes in temp, not original one. in above case temp list cleared first , run loop check search text in original list if matches add object temp list, , called notifydatasetchanged.. voila
Comments
Post a Comment