android - Clearing searchable listview -
i following android hive tutorial make custom list view + searchable listview . tutorials easy follow when added getfilter() in lazyadapter , problem occured. seach editbox can filter listview on clearing textview original data of listview not showing.
this code
package com.example.androidhive; import java.util.arraylist; import java.util.hashmap; import android.app.activity; import android.content.context; import android.text.textutils; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.textview; public class lazyadapter extends baseadapter { private activity activity; private arraylist<hashmap<string, string>> data; private static layoutinflater inflater=null; public imageloader imageloader; public lazyadapter(activity a, arraylist<hashmap<string, string>> d) { activity = a; data=d; inflater = (layoutinflater)activity.getsystemservice(context.layout_inflater_service); imageloader=new imageloader(activity.getapplicationcontext()); } public int getcount() { return data.size(); } public object getitem(int position) { return position; } public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { view vi=convertview; if(convertview==null) vi = inflater.inflate(r.layout.list_row, null); textview title = (textview)vi.findviewbyid(r.id.title); // title textview artist = (textview)vi.findviewbyid(r.id.artist); // artist name textview duration = (textview)vi.findviewbyid(r.id.duration); // duration imageview thumb_image=(imageview)vi.findviewbyid(r.id.list_image); // thumb image hashmap<string, string> song = new hashmap<string, string>(); song = data.get(position); // setting values in listview title.settext(song.get(customizedlistview.key_title)); artist.settext(song.get(customizedlistview.key_artist)); duration.settext(song.get(customizedlistview.key_duration)); imageloader.displayimage(song.get(customizedlistview.key_thumb_url), thumb_image); return vi; } public android.widget.filter getfilter() { // todo auto-generated method stub return new android.widget.filter() { @override protected filterresults performfiltering(charsequence constraint) { filterresults result = new filterresults(); // if constraint empty return original names if (textutils.isempty(constraint)){ //result.values = storedata; result.count = data.size(); result.values = data; return result; } arraylist<hashmap<string, string>> filtered_names = new arraylist<hashmap<string, string>>(); string filterstring = constraint.tostring().tolowercase(); string filterablestring; for(int = 0; i<data.size(); i++){ hashmap<string,string> searchdata = data.get(i); string itemsearched = searchdata.get(customizedlistview.key_title); filterablestring = itemsearched; if(filterablestring.tolowercase().contains(filterstring)){ filtered_names.add(data.get(i)); log.e("added", string.valueof(filtered_names.size())); } } result.values = filtered_names; result.count = filtered_names.size(); log.e("results", result.values.tostring() + string.valueof(result.count)); return result; } @override protected void publishresults(charsequence constraint, filterresults results) { // todo auto-generated method stub arraylist<hashmap<string, string>> resultlist = (arraylist<hashmap<string, string>>) results.values; lazyadapter.this.data = resultlist; lazyadapter.this.notifydatasetchanged(); } }; } }
this line lazyadapter.this.data = resultlist;
changes value of data, when go in empty constraint part, not modifying current filtering.
keep reference first data set :
- declaring
private arraylist<hashmap<string, string>> origdata;
- affecting in constructor :
origdata = d
; - using in filter empty constraint case
like follows
result.count = origdata.size(); result.values = origdata;
Comments
Post a Comment