Android Search Function With Custom ListView Using BaseAdapter And Edit Text -
hi i've been searching google few hours looking ways implement search filter / function custom listview
my main xml file consists of edit text , list view item , single row xml consists of 2x textview , 1x image view
my problem cannot seem search work references, ideas or otherwise appreciated
nb app still under development please excuse incomplete stubs
here's code:
public class kantodex extends activity { listview l; edittext inputsearch; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.kantodex); l = (listview) findviewbyid(r.id.listview1); l.setadapter(new adapterhand(this)); inputsearch = (edittext) findviewbyid(r.id.edittext); inputsearch.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence cs, int arg1, int arg2, int arg3) { // when user changed text } @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 } }); } class viewholder { imageview myimage; textview mytitle; textview mydescription; viewholder(view v) { mytitle = (textview) v.findviewbyid(r.id.textview); mydescription = (textview) v.findviewbyid(r.id.textview2); myimage = (imageview) v.findviewbyid(r.id.imageview); } } class singlerow { string title; string description; int balls; singlerow(string title, string description, int balls) { this.title = title; this.description = description; this.balls = balls; } } class adapterhand extends baseadapter { private list<singlerow> list2 = null; private arraylist<singlerow> arraylist; arraylist<singlerow> list; context context; arraylist<singlerow> list1 = new arraylist<>(); adapterhand(context c) { context = c; list = new arraylist<>(); resources res = c.getresources(); string[] title = res.getstringarray(r.array.kantotitles); string[] description = res.getstringarray(r.array.kantoids); int[] balls = {r.drawable.kantobulbasaur, r.drawable.kantoivysaur, r.drawable.kantovenusaur, r.drawable.kantocharmander, r.drawable.kantocharmeleon, r.drawable.kantocharizard, r.drawable.kantosquirtle, r.drawable.kantowartortle, r.drawable.kantoblastoise, r.drawable.kantocaterpie, r.drawable.kantometapod, r.drawable.kantobutterfree, r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos, r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos, r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos, r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos,r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos,r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos,r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos,r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos, r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos,r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos,r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos,r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos,r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos, r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos,r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos,r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos,r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos,r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos, r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos,r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos,r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos,r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos,r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos, r.drawable.kanto, r.drawable.johto, r.drawable.hoenn, r.drawable.sinnoh, r.drawable.unova, r.drawable.kalos,r.drawable.kanto}; (int = 0; < 151; i++) { list.add(new singlerow(title[i], description[i], balls[i])); } } @override public int getcount() { return list.size(); } @override public object getitem(int i) { return list.get(i); } @override public long getitemid(int i) { return i; } @override public view getview(int i, view convertview, viewgroup viewgroup) { view row = convertview; viewholder holder = null; if (row == null) { layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); row = inflater.inflate(r.layout.single_row, null, false); holder = new viewholder(row); row.settag(holder); log.d("metadex", "creating"); } else { holder = (viewholder) row.gettag(); log.d("metadex", "recycling"); } singlerow temp = list.get(i); holder.mytitle.settext(temp.title); holder.mydescription.settext(temp.description); holder.myimage.setimageresource(temp.balls); return row; } } }
edit adapter code below search feature.
class adapterhand extends baseadapter { private arraylist<singlerow> list = new arraylist<singlerow>(); private arraylist<singlerow> filteredlist = new arraylist<singlerow>(); private boolean searchenabled = false; private string searchterm; // constructor // @override public int getcount() { if(searchenabled) return filteredlist.size(); return list.size(); } @override public view getview(int position, view convertview, viewgroup parent) { // viewholder pattern singlerow temp = searchenabled ? filteredlist.get(position) : list.get(position); // set text, etc. temp holder.mytitle.settext(temp.title); holder.mydescription.settext(temp.description); holder.myimage.setimageresource(temp.balls); return convertview; } public void setsearchenabled(boolean enabled, string text) { searchenabled = enabled; if(!searchenabled) { searchterm = ""; filteredlist.clear(); notifydatasetchanged(); return; } searchterm = text.tolowercase(); filter(); } private void filter() { filteredlist.clear(); if(searchterm.length() == 0) { filteredlist.addall(list); } else if(searchterm.length() == 1) { for(singlerow row : list) { if(row.gettitle().tolowercase().charat(0) == searchterm.tolowercase().charat(0)) filteredlist.add(row); } } else { for(singlerow row : list) { if(row.gettitle().tolowercase().contains(searchterm)) { filteredlist.add(row); } } } notifydatasetchanged(); } }
then put lines below in ontextchanged()
string searchtext = inputsearch.gettext().tostring(); madapter.setsearchenabled(true, searchtext); //madapter instance of adapterhand keep reference of in activity
to cancel search call
madapter.setsearchenabled(false, "");
Comments
Post a Comment