android - How to show recently added items first in recyclerview -
i'm creating app similar instagram app admin can add contents. used python django backend when add new item django database, comes last on recyclerview. want in way that, when add new item, comes first when open app way instagram shows recent items first. custom adapter class
public class dealadapter extends recyclerview.adapter<dealadapter.dealholder> { context context; arraylist<deal> arraylist; public dealadapter(context context, arraylist<deal> arraylist) { this.context = context; this.arraylist = arraylist; } @override public dealholder oncreateviewholder(viewgroup parent, int viewtype) { layoutinflater inflater = layoutinflater.from(context); view v = inflater.inflate(r.layout.deal_cardview, parent, false); dealholder dealholder = new dealholder(v); return dealholder; } @override public void onbindviewholder(dealholder holder, int position) { deal deal = arraylist.get(position); holder.title.settext(deal.gettitle()); picasso.with(context). load(deal.getimage()). placeholder(r.drawable.loading). error(android.r.drawable.stat_notify_error). into(holder.image); } @override public int getitemcount() { if(arraylist != null) { return arraylist.size(); }else { return 0; } } public class dealholder extends recyclerview.viewholder { textview title; imageview image; public dealholder(view itemview) { super(itemview); title = (textview) itemview.findviewbyid(r.id.tvtitle); image = (imageview) itemview.findviewbyid(r.id.ivimage); } } }
this main activity class
public class mainactivity extends appcompatactivity { recyclerview recyclerview; string tag = "mainactivity"; imagebutton ibmore; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); recyclerview = (recyclerview) findviewbyid(r.id.rvrecyclerview); linearlayoutmanager layoutmanager = new linearlayoutmanager(this); recyclerview.setlayoutmanager(layoutmanager); string url = "http://192.168.43.2/api/deals/?format=json"; stringrequest stringrequest = new stringrequest(request.method.get, url, new response.listener<string>() { @override public void onresponse(string response) { arraylist<deal> userlist = new jsonconverter<deal>(). toarraylist(response, deal.class); dealadapter adapter = new dealadapter(getapplicationcontext(), userlist); recyclerview.setadapter(adapter); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { log.d(tag, error.tostring()); toast.maketext(getapplicationcontext(), "something went wrong: " + "'" + error + "'", toast.length_long).show(); } }); mysingleton.getinstance(getapplicationcontext()).addtorequestqueue(stringrequest); } }
okay worked me. after getting json , converting arraylist, called in "collections.reverse(arraylist);" before adding adapter shown below.
new response.listener<string>() { @override public void onresponse(string response) { arraylist<deal> userlist = new jsonconverter<deal>(). toarraylist(response, deal.class); collections.reverse(userlist); dealadapter adapter = new dealadapter(getapplicationcontext(), userlist); recyclerview.setadapter(adapter); }
Comments
Post a Comment