android - i can't fetch data with retrofit -
i'm implement recycler view , use retrofit fetch data api data array json
after i'm implement retrofit object , inside loop have problem can't fetch data send data model , don't have error message , blank screen , message in logat
e/recyclerview: no adapter attached; skipping layout
this mainactivity :
public class mainactivity extends appcompatactivity { private recyclerview recyclerview; private progressbar progressbar; private linearlayoutmanager mlinearlayout; arraylist<model> list; adapter adapter; string url = "https://d17h27t6h515a5.cloudfront.net/"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.listitem); recyclerview = (recyclerview) findviewbyid(r.id.recycler_view); progressbar = (progressbar) findviewbyid(r.id.progressbar); mlinearlayout = new linearlayoutmanager(mainactivity.this, linearlayoutmanager.vertical, false); recyclerview.setlayoutmanager(mlinearlayout); list = new arraylist<>(); getretrofitarray(); adapter=new adapter(list ,mainactivity.this); recyclerview.setadapter(adapter); } public void getretrofitarray() { retrofit retrofit = new retrofit.builder() .baseurl(url) .addconverterfactory(gsonconverterfactory.create()) .build(); retrofitarrayapi servese =retrofit.create(retrofitarrayapi.class); call< list<recipe>> call=servese.getrecipedetails(); call.enqueue(new callback<list<recipe>>() { @override public void onresponse(call<list<recipe>> call, response<list<recipe>> response) { for(int i=0;i<response.body().size();i++){ list.add(new model(response.body().get(i).getname(),response.body().get(i).getservings(),model.image)); } adapter.notifydatasetchanged(); } @override public void onfailure(call<list<recipe>> call, throwable t) { log.e("main","retrifit error"+ t.tostring()); } }); }
my modele
try once:
public class mainactivity extends appcompatactivity { private recyclerview recyclerview; private progressbar progressbar; private linearlayoutmanager mlinearlayout; arraylist<model> list; adapter adapter; string url = "https://d17h27t6h515a5.cloudfront.net/"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.listitem); list = new arraylist<>(); recyclerview = (recyclerview) findviewbyid(r.id.recycler_view); progressbar = (progressbar) findviewbyid(r.id.progressbar); mlinearlayout = new linearlayoutmanager(mainactivity.this, linearlayoutmanager.vertical, false); recyclerview.setlayoutmanager(mlinearlayout); adapter=new adapter(list ,mainactivity.this); recyclerview.setadapter(adapter); getretrofitarray(); } public void getretrofitarray() { retrofit retrofit = new retrofit.builder() .baseurl(url) .addconverterfactory(gsonconverterfactory.create()) .build(); retrofitarrayapi servese =retrofit.create(retrofitarrayapi.class); call< list<recipe>> call=servese.getrecipedetails(); call.enqueue(new callback<list<recipe>>() { @override public void onresponse(call<list<recipe>> call, response<list<recipe>> response) { list=response.body(); adapter.notifydatasetchanged(); } @override public void onfailure(call<list<recipe>> call, throwable t) { log.e("main","retrifit error"+ t.tostring()); } }); }
let me know if works you.
Comments
Post a Comment