so problem when type in won't search. need basic search when type letter or number filter it.
this adapter class:
public class knjigesearchadapter extends arrayadapter<knjiga> implements filterable{ context context; private arraylist<knjiga> originalvalues; private namefilter filter; arraylist<knjiga> podaci = new arraylist<>(); public knjigesearchadapter(context context,arraylist<knjiga> knjigas){ super(context, 0, knjigas); this.context = context; this.podaci = knjigas; this.originalvalues = new arraylist<>(); this.originalvalues.addall(podaci); } @override public view getview(int position, view convertview, viewgroup parent) { knjiga knjiga = getitem(position); if (convertview == null){ convertview = layoutinflater.from(getcontext()).inflate(r.layout.search_knjiga_layout, parent, false); } textview tvnazivk = (textview)convertview.findviewbyid(r.id.textviewnazivknjige); textview tvautork = (textview)convertview.findviewbyid(r.id.textviewautorknjige); textview tvisbn = (textview)convertview.findviewbyid(r.id.textviewisbnknjige); textview tvgodina = (textview)convertview.findviewbyid(r.id.textviewgodinaizdknjige); textview tvkatk = (textview)convertview.findviewbyid(r.id.textviewkategorijaknjige); tvnazivk.settext(knjiga.getnaziv()); tvautork.settext(knjiga.getautor()); tvisbn.settext(knjiga.getisbn()); tvgodina.settext(string.valueof(knjiga.getgodinaizdavanja())); tvkatk.settext(knjiga.getkategorija()); return convertview; } @override public filter getfilter() { if(filter == null){ filter = new namefilter(); } return filter; } private class namefilter extends filter{ @override protected filterresults performfiltering(charsequence constraint) { constraint = constraint.tostring().tolowercase(); filterresults results = new filterresults(); if(constraint != null && constraint.tostring().length() > 0) { arraylist<knjiga> filtrirano = new arraylist<>(); for(int = 0, l = originalvalues.size(); < l; i++) { knjiga knjiga = originalvalues.get(i); if(knjiga.tostring().tolowercase().contains(constraint)) filtrirano.add(knjiga); } results.count = filtrirano.size(); results.values = filtrirano; } else { synchronized(this) { results.values = originalvalues; results.count = originalvalues.size(); } } return results; } @suppresswarnings("unchecked") @override protected void publishresults(charsequence constraint, filterresults results) { podaci = (arraylist<knjiga>)results.values; notifydatasetchanged(); clear(); for(int = 0, l = podaci.size(); < l; i++) add(podaci.get(i)); notifydatasetinvalidated(); } } }
this activity class:
public class korisniksearchknjigeactivity extends appcompatactivity { listview lvpretragaknjiga; searchview svsearch; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_korisnik_search_knjige); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { snackbar.make(view, "replace own action", snackbar.length_long) .setaction("action", null).show(); } }); pretragaknjiga(); } @targetapi(build.version_codes.honeycomb) public void pretragaknjiga() { lvpretragaknjiga = (listview) findviewbyid(r.id.listviewlistaknjiga); svsearch = (searchview) findviewbyid(r.id.searchviewsearch); mydbhelper helper = new mydbhelper(this); final knjigesearchadapter adapter = new knjigesearchadapter(this, helper.getallknjige()); lvpretragaknjiga.setadapter(adapter); svsearch.setonquerytextlistener( new searchview.onquerytextlistener() { @override public boolean onquerytextsubmit(string query) { return false; } @override public boolean onquerytextchange(string newtext) { adapter.getfilter().filter(newtext); return true; } } ); } }
the problem might you're filtering comparing kijiga.tostring()
typed, , doesn't have "content" string think has. see tostring returns, like: "[kijiga#1345]". instead of tostring, might want use getter. or override tostring.
also try change:
public boolean onquerytextsubmit(string query) { adapter.getfilter().filter(query); return true; }
and imho there's bug in emulator, not work (depending on hw keyboard settings), test on real device!
Comments
Post a Comment