java - Filter a ListView with custom adapter and custom row in a fragment -
i have listview custom adapter in fragment. works fine i'm trying implement way filtering list when typing in edittext (i've tried searchview did not work either). each row has 2 edittexts , imageview.
in oncreateview
:
lst=(listview)rootview.findviewbyid(r.id.lst); lst.settextfilterenabled(true); mainad = new marrayadapter(getactivity(),r.layout.row,downlist); //it's more complicated listview populated correctly lst.setadapter(mainad); lst.settextfilterenabled(true); edit = (edittext) rootview.findviewbyid(r.id.txtexample); edit.addtextchangedlistener(new textwatcher() { @override public void beforetextchanged(charsequence charsequence, int i, int i1, int i2) { } @override public void ontextchanged(charsequence charsequence, int i, int i1, int i2) { mainad.getfilter().filter(charsequence.tostring()); } @override public void aftertextchanged(editable editable) { } }); return rootview;
the marrayadapter
:
private class marrayadapter extends arrayadapter<exhibition> implements filterable{ context context; int layoutresourceid; arraylist<exhibition> list; valuefilter valuefilter; arraylist<exhibition> filterlist; marrayadapter(context context, int layoutresourceid, arraylist<exhibition> data){ super(context, layoutresourceid, data); this.context=context; this.layoutresourceid=layoutresourceid; this.list=data; this.filterlist =data; } @override @nonnull public view getview(int position, view convertview, @nonnull viewgroup parent) { view row; exhibition ex; if(convertview==null){ row= getactivity().getlayoutinflater().inflate(r.layout.row,parent,false); } else row = convertview; textview ttl = (textview)row.findviewbyid(r.id.title); textview descr = (textview)row.findviewbyid(r.id.descr); imageview header = (imageview)row.findviewbyid(r.id.hd); ex = downlist.get(position); ttl.settext(ex.name); descr.settext(ex.description); byte[] decoded = base64.decode(ex.image,base64.default); bitmap decodedbitmap = bitmapfactory.decodebytearray(decoded,0,decoded.length); header.setimagebitmap(decodedbitmap); return row; } @override public filter getfilter() { if (valuefilter == null) { valuefilter = new valuefilter(); } return valuefilter; } private class valuefilter extends filter { @override protected filterresults performfiltering(charsequence constraint) { string filtstring = constraint.tostring().tolowercase(); filterresults results = new filterresults(); if(constraint == null || constraint.length() == 0){ results.values = filterlist; results.count = filterlist.size(); } else { list<exhibition> nexhlist = new arraylist<>(); for(exhibition e : list){ if (e.getname().tolowercase().startswith(constraint.tostring().tolowercase())){ nexhlist.add(e); } } results.values= nexhlist; results.count=nexhlist.size(); } return results; } @override protected void publishresults(charsequence constraint, filterresults results) { if(results.count==0){ notifydatasetinvalidated(); } else{ list = (arraylist<exhibition>)results.values; notifydatasetchanged(); } } } }
i don't know why it's not working. in advance.
i solved problem.
i add changes did make works but, if have same problem, note @ this link there example made filter correctly listview edittext , searchview.
for first, in edit.addtextchangedlistener
:
edit.addtextchangedlistener(new textwatcher() { @override public void beforetextchanged(charsequence charsequence, int i, int i1, int i2) { } @override public void ontextchanged(charsequence charsequence, int i, int i1, int i2) { if (i2 < i1) { mainad.resetdata(); //if delete characters show original list } mainad.getfilter().filter(charsequence.tostring()); } @override public void aftertextchanged(editable editable) { } });
for second, in marrayadapter
checked right variables (for example, calling original arraylist in getview()
) , add 3 methods needed:
@override public int getcount(){ return list.size(); } @override public exhibition getitem (int pos){ return list.get(pos); } void resetdata() { list = originallist; }
(i changed name of filterlist
in originallist
)
the valuefilter
following:
private class valuefilter extends filter { @override protected filterresults performfiltering(charsequence constraint) { filterresults results = new filterresults(); if(constraint == null || constraint.length() == 0){ results.values = originallist; results.count = originallist.size(); } else { list<exhibition> nexhlist = new arraylist<>(); for(exhibition e : list){ if (e.getname().touppercase().startswith(constraint.tostring().touppercase())){ nexhlist.add(e); } } results.values= nexhlist; results.count=nexhlist.size(); } return results; } @override protected void publishresults(charsequence constraint, filterresults results) { if(results.count==0){ notifydatasetinvalidated(); } else{ list = (arraylist<exhibition>)results.values; notifydatasetchanged(); } } }
Comments
Post a Comment