How to apply searching facility to a listview in android -
i want know how add search functionality listview in android here listview being loaded internet. essential ,thank
please @ psudo code:--- public class searchableadapter extends baseadapter implements filterable { private list<string>originaldata = null; private list<string>filtereddata = null; private layoutinflater minflater; private itemfilter mfilter = new itemfilter(); public searchableadapter(context context, list<string> data) { this.filtereddata = data ; this.originaldata = data ; minflater = layoutinflater.from(context); } public int getcount() { return filtereddata.size(); } public object getitem(int position) { return filtereddata.get(position); } public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { // viewholder keeps references children views avoid unnecessary calls // findviewbyid() on each row. viewholder holder; // when convertview not null, can reuse directly, there no need // reinflate it. inflate new view when convertview supplied // listview null. if (convertview == null) { convertview = minflater.inflate(r.layout.list_item, null); // creates viewholder , store references 2 children views // want bind data to. holder = new viewholder(); holder.text = (textview) convertview.findviewbyid(r.id.list_view); // bind data efficiently holder. convertview.settag(holder); } else { // viewholder fast access textview // , imageview. holder = (viewholder) convertview.gettag(); } // if weren't re-ordering rely on set last time holder.text.settext(filtereddata.get(position)); return convertview; } static class viewholder { textview text; } 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(); } }
}
//in activity or fragment of adapter instantiated :
edittxt.addtextchangedlistener(new textwatcher() {
@override public void ontextchanged(charsequence s, int start, int before, int count) { system.out.println("text ["+s+"]"); msearchableadapter.getfilter().filter(s.tostring()); } @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void aftertextchanged(editable s) { }
});
Comments
Post a Comment