java - Implementing Filterable on Array Adapter -
i need in implementing filterable interface list view items in note app working on. have tried several ways not getting expected result. nothing after tying search item. have implemented necessary methods in adapter class , main activity. need quite new this.thanks in anticipation positive replies.
// note.java public class note implements serializable { private long mdatetime; //creation time of note private string mtitle; //title of note private string mcontent; //content of note public note(long dateinmillis, string title, string content) { mdatetime = dateinmillis; mtitle = title; mcontent = content; } public void setdatetime(long datetime) { mdatetime = datetime; } public void settitle(string title) { mtitle = title; } public void setcontent(string content) { mcontent = content; } public long getdatetime() { return mdatetime; } } public string gettitle() { return mtitle; } public string getcontent() { return mcontent; } } // arrayadapter implements filterable class notelistadapter extends arrayadapter<note> implements filterable{ list<note> objects; private list<note> mstringfilterlist; filter filter; private static final int wrap_content_length = 5; public notelistadapter(context context, int resource, list<note> objects) { super(context, resource, objects); this.objects = objects; this.mstringfilterlist = objects; } @override public int getcount() { return objects.size(); } @nullable @override public note getitem(int position) { return objects.get(position); } @override public long getitemid(int position) { return position; } @override public filter getfilter() { filter = new filter() { @override protected filterresults performfiltering(charsequence constraint) { arraylist<note> templist=new arraylist<note>(); filterresults results = new filterresults(); if (constraint != null && objects != null) { for(note singlenote : objects) { if( singlenote.gettitle().contains(constraint)) templist.add(singlenote); } results.values = templist; results.count = templist.size(); } return results; } @override protected void publishresults(charsequence constraint, filterresults results) { objects = (arraylist<note>) results.values; notifydatasetchanged(); } }; return filter; } @override public view getview(int position, view convertview, viewgroup parent) { if(convertview == null) { convertview = layoutinflater.from(getcontext()) .inflate(r.layout.list_component, parent, false); } return convertview; } } // main activity @override public boolean onquerytextsubmit(string query) { arraylist<note> notes = utilities.getallsavednotes(getapplicationcontext()); final notelistadapter na = new notelistadapter(this, r.layout.list_component, notes); na.getfilter().filter(query); return false; } @override public boolean onquerytextchange(string newtext) { return false; } }
you're returning super.getfilter() instead of filter created. , also, you're not filtering in filter performfiltering()
method you're adding every item results if charsequence
has , adapter has items.
i suggest that:
protected filterresults performfiltering(charsequence constraint) { arraylist<note> templist=new arraylist<note>(); filterresults results = new filterresults(); if (constraint != null && objects != null) { for(note singlenote : objects) { if( singlenote.gettitle().contains(constraint) templist.add(singlenote); } results.values = templist; results.count = templist.size(); } return results; }
edits:
also, in activity, inside
@override public boolean onquerytextsubmit(string query) { arraylist<note> notes = utilities.getallsavednotes(getapplicationcontext()); final notelistadapter na = new notelistadapter(this, r.layout.list_component, notes); na.getfilter().filter(query); return false; }
you're using new adapter without setting recyclerview/listview. try using adapter create inside oncreate()
of activity or try using recyclerview/listview
.setadapter(na)
after change adapter using na.getfilter().filter(query);
edits2:
so, if want filter performed whenever user input move code onquerytextsubmit()
onquerytextchanged()
.
for improvement asked (when string empty reload full list) have 2 ways:
first way:
@override public boolean onquerytextchanged(string query) { if (!query.equals("")) { arraylist<note> notes = utilities.getallsavednotes(getapplicationcontext()); final notelistadapter na = new notelistadapter(this, r.layout.list_component, notes); na.getfilter().filter(query); } else { arraylist<note> notes = utilities.getallsavednotes(getapplicationcontext()); final notelistadapter na = new notelistadapter(this, r.layout.list_component, notes); } return false; }
the other way inside performfiltering()
method of filter interface implementation. check if string empty there, , if need return full list, without creating new support 1 in add elements if condition match.
Comments
Post a Comment