Android: Filtering in the Adapter does not working -
i have following class filtering called on onquerytextchange. problem found results not updated in list of found results. example have list (see. image below):
and entered query "test 1", updated result should contain 1 found row.
but result list still same.
i ask should update filtered results in right way.
many advice.
mainactivity
searchview.setonquerytextlistener(new searchview.onquerytextlistener() { @override public boolean onquerytextsubmit(string s) { logger.d("onquerytextsubmit "); return false; } @override public boolean onquerytextchange(string s) { logger.d(s); logger.d("onquerytextchange "); madapter = new wlannetworkadapter(datacontainer.getaccesspointlist(), getparent()).getfilter(); madapter.filter(s.tostring()); //todo: https://coderwall.com/p/zpwrsg/add-search-function-to-list-view-in-android return true; } });
adapter:
public class wlannetworkadapter extends baseadapter implements filterable { private list<wlannetwork> maccesspointlist; private activity context; private filter mfilter; layoutinflater inflater; private list<wlannetwork> filteredwlanlist; public wlannetworkadapter(list<wlannetwork> maccesspointlist, activity context) { this.maccesspointlist = maccesspointlist; this.context = context; } @override public int getcount() { return maccesspointlist.size(); } @override public object getitem(int arg0) { return maccesspointlist.get(arg0); } @override public long getitemid(int arg0) { // todo auto-generated method stub return arg0; } @override public view getview(int arg0, view rowview, viewgroup arg2) { //with simple check, can save overhead of inflating new view each row. //this make our listview faster. if (rowview == null) { layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); rowview = inflater.inflate(r.layout.list_item_found_wlan_data, arg2, false); } // set text values textviews in item textview ssidtv = (textview) rowview.findviewbyid(r.id.found_wlan_ssid_tv); ssidtv.settext(maccesspointlist.get(arg0).getssid()); textview passwordtv = (textview) rowview.findviewbyid(r.id.found_wlan_password_tv); passwordtv.settext(maccesspointlist.get(arg0).getpsk()); return rowview; } @override public filter getfilter() { filter filter = new filter() { @suppresswarnings("unchecked") @override protected void publishresults(charsequence constraint, filterresults results) { maccesspointlist = (list<wlannetwork>) results.values; // has filtered values notifydatasetchanged(); // notifies data new filtered values } @override protected filterresults performfiltering(charsequence constraint) { filterresults results = new filterresults(); // holds results of filtering operation in values // implement here filter logic if (constraint == null || constraint.length() == 0) { logger.d("search query empty, display values"); // no filter implemented return list results.values = foundcontroller.getaccesspointslist(); results.count = foundcontroller.getaccesspointslist().size(); } else { logger.d("search query is: " + constraint.tostring()); // perform filtering operation filteredwlanlist = new arraylist<wlannetwork>(); (wlannetwork wlannetwork : foundcontroller.getaccesspointslist()) { if (wlannetwork.getssid().touppercase().startswith(constraint.tostring().touppercase())) { logger.d("found"); filteredwlanlist.add(wlannetwork); } } logger.d("count :"); logger.d(string.valueof(filteredwlanlist.size())); results.values = filteredwlanlist; results.count = filteredwlanlist.size(); } return results; } }; return filter; } }
change mainactivity code :
searchview.setonquerytextlistener(new searchview.onquerytextlistener() { @override public boolean onquerytextsubmit(string s) { logger.d("onquerytextsubmit "); return false; } @override public boolean onquerytextchange(string s) { if (adapter != null) adapter.getfilter().filter(s); return false; } });
Comments
Post a Comment