android - Get details from recyclerview and show result in another fragment using cardview with retrofit 2 networking library -
iam using cardview in recyclerview list result of retrofit request, next want details of cardview clicked , display details of cardview (json) in fragment (to concise i'm using fragments listing , showing details)
the below code first fragment in i'm fetching json using retrofit 2. code working fine, giving out put json.
public class agentassetsfragment extends fragment implements view.onclicklistener { public textview titlename, assetrate, assetbrand; public recyclerview assetrecyclerview; list<agentasset> data; agentassetsadapter agentassetsadapter; framelayout mloadingframe; public sharedpreferences pref; public agentassetsfragment() { // required empty public constructor } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_agent_assets, container, false); assetrecyclerview = (recyclerview) view.findviewbyid(r.id.agentassetlistrecyclerid); titlename = (textview) view.findviewbyid(r.id.assettitleid); assetrate = (textview) view.findviewbyid(r.id.assetrateid); assetbrand = (textview) view.findviewbyid(r.id.assetbrandid); mloadingframe = (framelayout) view.findviewbyid(r.id.agentloadingframe); initviews(); mloadingframe.setvisibility(view.visible); return view; } private void initviews() { assetrecyclerview.sethasfixedsize(true); gridlayoutmanager assetgridmanager = new gridlayoutmanager(getcontext(), 2); assetrecyclerview.setlayoutmanager(assetgridmanager); assetrecyclerview.setadapter(agentassetsadapter); getassets(); } private void getassets() { httplogginginterceptor logging = new httplogginginterceptor(); logging.setlevel(httplogginginterceptor.level.body); okhttpclient.builder httpclient = new okhttpclient.builder(); httpclient.readtimeout(60, timeunit.seconds); httpclient.connecttimeout(60, timeunit.seconds); pref = this.getactivity().getsharedpreferences("loginactivity", context.mode_private); final string actoken = pref.getstring("token", "default"); httpclient.addinterceptor(new interceptor() { @override public okhttp3.response intercept(chain chain) throws ioexception { request original = chain.request(); request request = original.newbuilder() .header("authorization", "bearer " + actoken) .method(original.method(), original.body()) .build(); return chain.proceed(request); } }); okhttpclient client = httpclient.build(); retrofit retrofit = new retrofit.builder() .baseurl(constants.base_url) .addconverterfactory(gsonconverterfactory.create()) .client(client) .build(); final apiinterface apiinterface = retrofit.create(apiinterface.class); final assetrequest assetrequest = new assetrequest(); final call<list<agentasset>> call = apiinterface.getassetlistofagent(); call.enqueue(new callback<list<agentasset>>() { @override public void onresponse(call<list<agentasset>> call, response<list<agentasset>> response) { int statuscode = response.code(); if (statuscode == 200 && response.body() != null) { list<agentasset> agentassets = response.body(); data = agentassets; agentassetsadapter = new agentassetsadapter(data); id = agentassetsadapter.agentassetid; assetrecyclerview.setadapter(agentassetsadapter); toast.maketext(getcontext(), data.size() + " results", toast.length_short).show(); } else if (statuscode == 406) { toast.maketext(getcontext(), "your token expired " + statuscode, toast.length_long).show(); } else if (response.body() == null) { toast.maketext(getcontext(), "no results " + statuscode, toast.length_short).show(); } else { toast.maketext(getcontext(), "network error, try again later " + statuscode, toast.length_long).show(); } mloadingframe.setvisibility(view.invisible); } @override public void onfailure(call<list<agentasset>> call, throwable t) { mloadingframe.setvisibility(view.invisible); toast.maketext(getcontext(), "error, check network ", toast.length_long).show(); log.d("agentasset failure", "onfailure: " + t.getmessage()); } }); } @override public void onclick(view v) { } }
this adapter class inflating recyclerview, in using cardview indicate each item. able position of item clicked, when try id of item showing id of last item in list. dont know why, tried every question in stackoverflow morning, didn't find result query.
public class agentassetsadapter extends recyclerview.adapter<agentassetsadapter.viewholder> { public list<agentasset> agentassetlists; private static agentassetclicklistener agentassetclicklistener; integer agentassetid; string href; public agentassetsadapter(list<agentasset> agentassetlists) { this.agentassetlists = agentassetlists; } @override public int getitemcount() { return agentassetlists.size(); } public interface agentassetclicklistener { void onitemclick(int position, view v); } @override public agentassetsadapter.viewholder oncreateviewholder(viewgroup viewgroup, int i) { view view = layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.agent_asset_card, viewgroup, false); return new viewholder(view); } @override public void onbindviewholder(agentassetsadapter.viewholder holder, int position) { if (agentassetlists != null) { holder.titlename.settext(agentassetlists.get(position).getname()); holder.assetbrand.settext(agentassetlists.get(position).getbrand().getname()); agentassetid = agentassetlists.get(position).getidbrand(); href = agentassetlists.get(position).getlinks().getself().gethref(); if (agentassetlists.get(position).getrank() == -1) { holder.agentrank.settext(""); } else { holder.agentrank.settext(string.valueof(agentassetlists.get(position).getrank())); } if (objects.equals(agentassetlists.get(position).getrate().getavailable().tostring(), "0")) { holder.assetrate.settext("nil"); } else { holder.assetrate.settext(string.valueof(agentassetlists.get(position).getrate().getvalue())); } if (objects.equals(agentassetlists.get(position).getbestrate().getavailable().tostring(), "0")) { holder.agentrate.settext("nil"); } else { holder.agentrate.settext(string.valueof(agentassetlists.get(position).getbestrate().getvalue())); } } else { holder.resultcount.settext("no assets, try later"); } } public void additems(list<agentasset> asresponse, int index) { int cursize = agentassetlists.size(); agentassetlists.addall(index, asresponse); if (getitemcount() != 0) { notifyitemrangeinserted(index, (agentassetlists.size() - cursize)); } else { notifydatasetchanged(); } } public class viewholder extends recyclerview.viewholder implements view.onclicklistener { textview titlename, assetrate, assetbrand, agentrate, agentrank, resultcount; imageview assetimage; public viewholder(view view) { super(view); resultcount = (textview) view.findviewbyid(r.id.resultcountid); titlename = (textview) view.findviewbyid(r.id.agentassettitleid); assetrate = (textview) view.findviewbyid(r.id.assetrateid); assetbrand = (textview) view.findviewbyid(r.id.agentassetbrandid); agentrank = (textview) view.findviewbyid(r.id.agentrankid); agentrate = (textview) view.findviewbyid(r.id.agentassetrateid); itemview.setonclicklistener(this); //img_android = (imageview) view.findviewbyid(r.id.img_android); } @override public void onclick(view v) { //log.d("item clicked", "item clicked" + getadapterposition() ); toast.maketext(v.getcontext(), "clicked aaa " + getadapterposition(), toast.length_short).show(); } } public void setonitemclicklistener(agentassetclicklistener agentassetclicklistener) { agentassetsadapter.agentassetclicklistener = agentassetclicklistener; } @override public void onattachedtorecyclerview(recyclerview recyclerview) { super.onattachedtorecyclerview(recyclerview); } }
how id item clicked ? , how pass data next fragment shows complete details of current clicked cardview ? appreciated.
in advance.
edit got attach json response, here it
[ { "id_asset": 1, "id_category": 1, "id_brand": 2, "name": "samsung galaxy 6", "status": 1, "updated_at": "oct 3, 2016 10:24:28 am", "rank": 1, "rate": { "available": 1, "id_asset_rate": 2, "id_asset": 1, "id_user": 10, "value": 5000, "loan_to_value": 50, "offered": 2500, "annual_rate": 3, "quantity": 5, "status": 1, "created_at": 1477200691, "updated_at": 1477200751 }, "best_rate": { "available": 1, "id_asset": 1, "value": 5000, "loan_to_value": 50, "offered": 2500, "annual_rate": 3, "quantity": 5, "rank": 1 }, "category": { "id_category": 1, "id_parent": 0, "name": "mobile phones", "image": "", "sort": 1, "status": 1, "created_at": null, "updated_at": null, "_links": { "self": { "href": "/v1/categories/1" } } }, "brand": { "id_brand": 2, "name": "samsung", "status": 1, "created_at": null, "updated_at": null }, "_links": { "self": { "href": "/v1/assets/1" } } } ]
you overwrite id on every onbind in adapter:
here should null, because id never set before adapter set recyclerview:
@override public void onresponse(call<list<agentasset>> call, response<list<agentasset>> response) { //... if (statuscode == 200 && response.body() != null) { //... agentassetsadapter = new agentassetsadapter(data); id = agentassetsadapter.agentassetid; //<--- //... } //... }
here overwrite every bind iteration asset id:
@override public void onbindviewholder(agentassetsadapter.viewholder holder, int position) { if (agentassetlists != null) { holder.titlename.... holder.assetbrand.... //here overwrite id on every iteration. agentassetid = agentassetlists.get(position).getidbrand(); href = .... } }
the result you id of latest binded asset item in adapter.
this solution:
adjust interface:
public interface agentassetclicklistener { void onitemclick(int position, agentasset item); }
implement interface:
public class agentassetsfragment extends fragment implements agentassetsadapter.agentassetclicklistener { public textview titlename, assetrate, assetbrand; public recyclerview assetrecyclerview; list<agentasset> data; agentassetsadapter agentassetsadapter; framelayout mloadingframe; public sharedpreferences pref; public agentassetsfragment() { // required empty public constructor } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { //... initviews(); mloadingframe.setvisibility(view.visible); return view; } private void initviews() { assetrecyclerview.sethasfixedsize(true); gridlayoutmanager assetgridmanager = new gridlayoutmanager(getcontext(), 2); assetrecyclerview.setlayoutmanager(assetgridmanager); getassets(); } private void getassets() { //... call.enqueue(new callback<list<agentasset>>() { @override public void onresponse(call<list<agentasset>> call, response<list<agentasset>> response) { int statuscode = response.code(); if (statuscode == 200 && response.body() != null) { list<agentasset> agentassets = response.body(); data = agentassets; agentassetsadapter = new agentassetsadapter(data); agentassetsadapter.setonitemclicklistener(agentassetsfragment.this); assetrecyclerview.setadapter(agentassetsadapter); //id = agentassetsadapter.agentassetid; //gon't know use this... toast.maketext(getcontext(), data.size() + " results", toast.length_short).show(); } else if //... mloadingframe.setvisibility(view.invisible); } @override public void onfailure(call<list<agentasset>> call, throwable t) { //... } }); } @override public void onitemclick(int position, agentasset item) { //item.getlinks().getself().gethref(); //item.getassetid(); //log.d("item clicked", "item clicked" + getadapterposition() ); toast.maketext(getcontext(), "asset["+position+"] clicked: " + item, toast.length_short).show(); } }
adjust bind method in recycler adapter;
public class agentassetsadapter extends recyclerview.adapter<agentassetsadapter.viewholder> { //why static? //private static agentassetclicklistener agentassetclicklistener; //integer agentassetid; //string href; public list<agentasset> agentassetlists; private agentassetclicklistener agentassetclicklistener; public agentassetsadapter(list<agentasset> agentassetlists) { this.agentassetlists = agentassetlists; } @override public int getitemcount() { return agentassetlists.size(); } public interface agentassetclicklistener { void onitemclick(int position, view v); } @override public agentassetsadapter.viewholder oncreateviewholder(viewgroup viewgroup, int i) { view view = layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.agent_asset_card, viewgroup, false); return new viewholder(view); } @override public void onbindviewholder(agentassetsadapter.viewholder holder, int position) { if (agentassetlists != null) { agentasset agenta = agentassetlists.get(position); holder.titlename.settext(agenta.getname()); holder.assetbrand.settext(agenta.getbrand().getname()); //global values overwritten every new agenta, need this? //href = agenta.getlinks().getself().gethref(); //agentassetid = agenta.getidbrand(); //set onlick listener itemview , call agentassetclicklistener.onitemclick(); holder.itemview.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view v) { if(agentassetclicklistener != null){ agentassetclicklistener.onitemclick(holder.getadapterposition(), agenta); } } }); if (agenta.getrank() == -1) { holder.agentrank.settext(""); } else { holder.agentrank.settext(string.valueof(agenta.getrank())); } if (objects.equals(agenta.getrate().getavailable().tostring(), "0")) { holder.assetrate.settext("nil"); } else { holder.assetrate.settext(string.valueof(agenta.getrate().getvalue())); } if (objects.equals(agenta.getbestrate().getavailable().tostring(), "0")) { holder.agentrate.settext("nil"); } else { holder.agentrate.settext(string.valueof(agenta.getbestrate().getvalue())); } } else { holder.resultcount.settext("no assets, try later"); } } public void additems(list<agentasset> asresponse, int index) { //... } public class viewholder extends recyclerview.viewholder implements view.onclicklistener { textview titlename, assetrate, assetbrand, agentrate, agentrank, resultcount; imageview assetimage; public viewholder(view view) { super(view); resultcount = (textview) view.findviewbyid(r.id.resultcountid); titlename = (textview) view.findviewbyid(r.id.agentassettitleid); assetrate = (textview) view.findviewbyid(r.id.assetrateid); assetbrand = (textview) view.findviewbyid(r.id.agentassetbrandid); agentrank = (textview) view.findviewbyid(r.id.agentrankid); agentrate = (textview) view.findviewbyid(r.id.agentassetrateid); //no need anymore //itemview.setonclicklistener(this); //img_android = (imageview) view.findviewbyid(r.id.img_android); } } public void setonitemclicklistener(agentassetclicklistener agentassetclicklistener) { this.agentassetclicklistener = agentassetclicklistener; } }
the listener of agentassetclicklistener gets item via onitemclick(int, agentasset) can item brand id via getter method of agentasset class. if id still same, there issue in data structure creation.
edit: extended answer better understanding.
Comments
Post a Comment