search - Searching a custom listview using an ArrayAdapter in Android -
i have custom listview has 2 textviews , 1 imageview in it. uses custom arrayadapter it's adapter, , wanted add search function it. i've looked through every single tutorial find, no luck. 1 tutorial able search through regular listview (not using custom adapter), not work me because need show both textviews , imageview. need filter 1 of textviews though, don't see how trouble be.
anyway, here mainactivity:
public class itemid extends activity { // list view private listview lv; // listview adapter arrayadapter<string> adapter; // search edittext edittext inputsearch; // arraylist listview arraylist<hashmap<string, string>> productlist; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.testo); context ctx = getapplication(); resources res = ctx.getresources(); string[] options = res.getstringarray(r.array.item_names); lv = (listview) findviewbyid(r.id.list_view); inputsearch = (edittext) findviewbyid(r.id.inputsearch); // adding items listview adapter = new arrayadapter<string>(this, r.layout.list_item_test, r.id.product_name, options); lv.setadapter(adapter); /** * enabling search filter * */ inputsearch.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence cs, int arg1, int arg2, int arg3) { // when user changed text itemid.this.adapter.getfilter().filter(cs); } @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 } }); } }
and here custom adapter:
public class itemidadapter extends arrayadapter<string> implements filterable { public layoutinflater minflater; public string[] mstrings; public string[] mids; public typedarray micons; public int mviewresourceid; public itemidadapter(context ctx, int viewresourceid, string[] strings, string[] ids, typedarray icons) { super(ctx, viewresourceid, strings); minflater = (layoutinflater)ctx.getsystemservice( context.layout_inflater_service); mstrings = strings; mids = ids; micons = icons; mviewresourceid = viewresourceid; } @override public int getcount() { return mstrings.length; } @override public string getitem(int position) { return mstrings[position]; } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { convertview = minflater.inflate(mviewresourceid, null); imageview iv = (imageview)convertview.findviewbyid(r.id.option_icon); iv.setimagedrawable(micons.getdrawable(position)); textview tv = (textview)convertview.findviewbyid(r.id.option_text); tv.settext(mstrings[position]); textview tv1 = (textview)convertview.findviewbyid(r.id.itemids); tv1.settext(mids[position]); return convertview; } }
hopefully can me, has been tedious challenge because i'm new android development, , i'm out of options. help!
in oncreate():
inputsearch = (edittext)findviewbyid(r.id.inputsearch); inputsearch.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence cs, int arg1, int arg2, int arg3) { // when user changed text youradaptername.getfilter().filter(cs); } @override public void beforetextchanged(charsequence arg0, int arg1, int arg2, int arg3) { // todo auto-generated method stub } @override public void aftertextchanged(editable s) { // todo auto-generated method stub } });
and in layout:
<edittext android:id="@+id/inputsearch" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="search.." android:inputtype="textvisiblepassword"/>
Comments
Post a Comment