Android Filter ListView Using BaseAdapter -
we need implement code of filter list view in example. using 2 classes : car.java
(contains object car), class caradapter
extends baseadapter
class. below of codes:
car.java
public class car { public string title; public int car_id; public car(string title,int car_id) { this.title = title; this.car_id = car_id; } }
caradapter.java
public class caradapter extends baseadapter { private list<car> mcarlist; private layoutinflater minflater; public caradapter(list<car> list, layoutinflater inflater) { mcarlist = list; minflater = inflater; } @override public int getcount() { return mcarlist.size(); } @override public object getitem(int position) { return mcarlist.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { final viewitem item; if (convertview == null) { convertview = minflater.inflate(r.layout.prototype, null); item = new viewitem(); item.cartitle = (textview) convertview .findviewbyid(r.id.textviewprototype); convertview.settag(item); } else { item = (viewitem) convertview.gettag(); } car curcar = mcarlist.get(position); item.cartitle.settext(curcar.title); return convertview; } private class viewitem { textview cartitle; } }
caractivity.java
public class activitycar extends activity { private list<car> mcarlist; private edittext et; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.car); // obtain reference product catalog mcarlist = carhelper.getcar(getresources()); // create list listview listviewcatalog = (listview) findviewbyid(r.id.list_car); listviewcatalog.setadapter(newcaradapter(mcarlist,getlayoutinflater))); } }
thank help.
follow below steps make listview filterable:
in
caradapter.java
implement filterable interface. take @ similar question answered here.in
caractivity.java
set listview adapter filterable.
listviewcatalog.settextfilterenabled(true);
if had used
android.widget.searchview
implement searchview.onquerytextlistener or if usededittext
implement textwatcher interface.
Comments
Post a Comment