Filter method return just the first item Android -
i have recyclerview
, tried make filter search recycler item , working , when use , type thing result first item of list.
this filter method
private list<data> filter(list<data> datas, string newtext) { newtext = newtext.tolowercase(); final list<data> filteredmodellist = new arraylist<>(); (data data : datas) { final string text = data.gettodo_title().tolowercase(); if (text.contains(newtext)) { filteredmodellist.add(data); } } return filteredmodellist; }
and menu
@override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.menu_search, menu); menuitem searchitem = menu.finditem(r.id.action_search); searchview searchview = (searchview) menuitemcompat.getactionview(searchitem); searchview.setonquerytextfocuschangelistener(new view.onfocuschangelistener() { @override public void onfocuschange(view v, boolean hasfocus) { } }); searchview.setonquerytextlistener(new searchview.onquerytextlistener() { @override public boolean onquerytextsubmit(string query) { return true; } @override public boolean onquerytextchange(final string searchquery) { final list<data> filteredmodellist = filter(dblist, searchquery); filteredmodellist.indexof(searchquery); list <data> search =filteredmodellist; todorecyclerview.scrolltoposition(0); recycleradapter = new todorecycleradapter(search); recycleradapter.notifydatasetchanged(); return false;}}); menuitemcompat.setonactionexpandlistener(searchitem, new menuitemcompat.onactionexpandlistener() { @override public boolean onmenuitemactioncollapse(menuitem item) { return true; } @override public boolean onmenuitemactionexpand(menuitem item) { return true; } }); return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_search) { return true; } return super.onoptionsitemselected(item); }}
why creating new instance of adapter everytime search?
recycleradapter = new todorecycleradapter(search);
instead create in once , call recycleradapter.notifydatasetchanged();
Comments
Post a Comment