android - notifyDataSetChanged not working -
my app uses multicolumnlistview
(https://github.com/huewu/pinterestlikeadapterview) , name says helps creating multi column list views.
the library great, specify, filtering not supported.
so trying create own text-filter functionality simple edit text.
finally achieve filter list need refresh listview , recall setadapter because pass list on it.
myadapter adapter = new myadapter(this,mylist); listview.setadapter(adapter);
when execute listview.invalidateviews()
or adapter.notifydatasetchanged()
listview refreshed old list.
which best way of recalling setadapter? or maybe way of doing this..
thanks in advance
edit:
//method filters list log.i("on filter mylist.size()",""+mylist.size()); adapter.notifydatasetchanged(); //on adapter log.i("on adapter mylist.size()",""+mylist.size());
log:
adapter class:
public class miadaptadorlistacomercios extends baseadapter{ //textview , imageviews declarations private context contexto; private list<comercio> mylist; public miadaptadorlistacomercios(context c,list<comercio> mylist){ this.contexto = c; this.mylist = new arraylist<comercio>(); this.mylist = mylist; } @override public int getcount() { log.i("on adapter mylist.size()",""+listacomercios.size()); return mylist.size(); } @override public object getitem(int position) { return mylist.get(position); } @override public long getitemid(int arg0) { // todo auto-generated method stub return 0; } @override public view getview(int position, view convertview, viewgroup parent) { view view =null; if(convertview == null) { layoutinflater inflater = (layoutinflater) contexto.getsystemservice(context.layout_inflater_service); view = inflater.inflate(r.layout.list_cell, null); } else{ view = convertview; } //set views texteviews text,font etc... return view; }
}
activity class:
public class listacomercio extends activity { private multicolumnlistview madapterview = null; edittext searchbar; private arraylist<comercio> mylist; private arraylist<comercio> filteredlist; myadapter adapter; public listacomercio(){ mylist = new arraylist<comercio>(); filteredlist = new arraylist<comercio>(); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.lista_comercios); madapterview = (multicolumnlistview) findviewbyid(r.id.list); adapter = new myadapter(listacomercio.this,mylist); madapterview.setadapter(adapter); searchbar.setoneditoractionlistener(new textview.oneditoractionlistener() { @override public boolean oneditoraction(textview v, int actionid, keyevent event) { if (actionid == editorinfo.ime_action_search) { charsequence filer = v.gettext(); for(comercio co : mylist) { if(co.getname().contains(filer)) { filteredlist.add(co); } } mylist = filteredlist; log.i("on filter mylist.size()",""+mylist.size()); mylist.clear(); adapter.notifydatasetchanged(); //madapterview.invalidateviews(); return true; } return false; } }); } }
just change mylist
, call adapter.notifydatasetchanged()
don't set new adapter each time.
in constructor of custom adapter call super
takes arraylist argument.
call this:
public miadaptadorlistacomercios(context c,list<comercio> mylist){ super(c,0,mylist); this.contexto = c; this.mylist = mylist; }
you can keep adapter problem in line:
mylist = filteredlist;
instead of changing reference should change list itself.
mylist.clear(); mylist.addall(filteredlist);
by doing loose original list suggest keeping list call ed originallist have complete list , initialize mylist in oncreate by:
mylist=new arraylist(originallist);
so every time want re-set call:
mylist.clear(); mylist.addall(originallist);
and in
for(comercio co : originallist) { if(co.getname().contains(filer)) { filteredlist.add(co); } }
Comments
Post a Comment