android - Update recycle view -
i have trouble recycleview. want update recycleview using diffutil. can't understand why not work. diffutil
public class consumablediffcallback extends diffutil.callback { private list<consumablebytask> oldlistofconsumablebytask; private list<consumablebytask> newlistofconsumablebytask; @override public boolean areitemsthesame(int olditemposition, int newitemposition) { return oldlistofconsumablebytask.get(olditemposition).getquantity() != newlistofconsumablebytask.get(newitemposition).getquantity(); } @override public boolean arecontentsthesame(int olditemposition, int newitemposition) { return oldlistofconsumablebytask.get(olditemposition).equals(newlistofconsumablebytask.get(newitemposition)); }}
my adapter holder
public class consumeholder extends recyclerview.viewholder { private button addconsumebtn; public consumeholder(view itemview) { /** * add/change data , send server. * after close consumablesbytaskactivity. */ addconsumebtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //do listener.senddataandcloseframe(); } });}}
update method in adapter class
public void updatelist(list<consumablebytask> list){ final consumablediffcallback consumablediffcallback = new consumablediffcallback(this.consumablebytasklist,list); final diffutil.diffresult diffresult = diffutil.calculatediff(consumablediffcallback); this.consumablebytasklist.clear(); this.consumablebytasklist.addall(list); diffresult.dispatchupdatesto(this); }
activity method
public void senddataandcloseframe() { list<consumablebytask> listofconsumable = //get new list server; adapter.updatelist(listofconsumable); finish(); }
thanks.
i'm using in recycleradapater
private list<bindableassignment> databaseitemlist; public void setitems(final list<bindableassignment> argumentlist) { if (databaseitemlist == null) { databaseitemlist = argumentlist; notifyitemrangechanged(0, databaseitemlist.size()); } else { diffutil.diffresult result = diffutil.calculatediff(new diffutil.callback() { @override public int getoldlistsize() { return databaseitemlist.size(); } @override public int getnewlistsize() { return argumentlist.size(); } @override public boolean areitemsthesame(int olditemposition, int newitemposition) { return databaseitemlist.get(olditemposition).getid() == argumentlist.get(newitemposition).getid(); } @override public boolean arecontentsthesame(int olditemposition, int newitemposition) { bindableassignment olditem = databaseitemlist.get(olditemposition); bindableassignment newitem = argumentlist.get(newitemposition); // change apply object return olditem.getid() == newitem.getid() && olditem.getname().equals(newitem.getname()) && (olditem.getdesc() != null && newitem.getdesc() != null) && olditem.getdesc().equals(newitem.getdesc()) && olditem.getclassid() == newitem.getclassid() && olditem.getduetime() == newitem.getduetime(); } }); databaseitemlist = argumentlist; result.dispatchupdatesto(this); } log.d("databindingadapter", "changing data " + databaseitemlist.size() + " elements"); }
hopefully works you.
Comments
Post a Comment