android - Changing a business object in an Adapter -
i'm building android app similar facebook (gets newsfeed stored in db rest api), , implementing reddit-like voting system, in every feed item has vote state (none, up, down) stored in db.
when retrieve newsfeed json, set votestate vote state in json, display through newsfeed adapter.
but if set votestate in newsfeed adapter value (based on onclicklistener), changed value not reach actual newsfeed. because every time scroll newsfeed, newsfeed adapter gets new instance of newsfeed, , doesn't care value changed.
thus, i'm looking best way modify permanently newsfeed vote state newsfeed adapter (this can generalized variable assigned arraylist , displayed through arraylistadapter).
if have suggestions, please feel free respond.
thank :)
edit :
public class newsfeedadapter extends recyclerview.adapter<newsfeedadapter.viewholder> { private newsfeed newsfeed; private feeditem item; public newsfeedadapter(..., newsfeed newsfeed) { ...; this.newsfeed = newsfeed; } public static class viewholder extends recyclerview.viewholder { protected button button_up/down.... public viewholder(view v) { super(v); button = findviewbyid ... (r.id.button); ... } } // create new views (invoked layout manager) @override public viewholder oncreateviewholder(viewgroup viewgroup, int viewtype) { ... } @override public void onbindviewholder(final viewholder holder, final int position) { final feeditem item = newsfeed.get(position); holder.button_up.settag(position); holder.button_up.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { changevote(item, "up"); } }); holder.button_down.settag(position); holder.button_down.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { changevote(item, "down"); } }); } public void changevote(feeditem item, string flag) { // log.d(tag, "state of vote :" + item.getvotestate()); if (item.getvotestate().equals(flag)) { item.setvotestate("none"); log.d(tag, "deleting vote :" + item.getvotestate()); new connexion(activity, fragment, "vote", "deleting vote...").execute(string.valueof(item.getid()), flag, "delete"); } else if (item.getvotestate().equals("none")){ item.setvotestate(flag); log.d(tag, "voting :" + item.getvotestate()); new connexion(activity, fragment, "vote", "voting...").execute(string.valueof(item.getid()), flag, "insert"); } else { item.invertvotestate(flag); log.d(tag, "inverting :" + item.getvotestate()); new connexion(activity, fragment, "vote", "voting...").execute(string.valueof(item.getid()), flag, "invert"); } } }
you have 2 options:
1) send updates rest api every time piece of data changed. expensive , not want likely.
2) create local data layer between rest api , listview - sqlite database or other orm lib. way can hold instance of db in adapter, , whenever piece of data changes, can update through simple db method call, , periodically send updates rest api, while checking new updates rest api , pushing them db.
i highly recommend using sqlite db, can keep counter in db code of number of changes, or time passed since last sync, , keep data user seeing date out smashing users data usage repeated network calls.
Comments
Post a Comment