android - Activity cant populate the recyclerView with json -
i write example app ny times api . when test json ,it's ok . in activity when receive json
. can't populate recyclerview
json . it's lucky recyclerview populate json , nothing . , when run app on emulator android monitor says :
11226-11226/com.nytimes.nytimes e/recyclerview:
no adapter attached; skipping layout
this postlistactivity.java
recyclerview populate json :
package com.nytimes.nytimes.activities; import android.content.intent; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.support.v7.widget.toolbar; import android.widget.toast; import com.android.volley.request; import com.android.volley.response; import com.android.volley.volleyerror; import com.android.volley.toolbox.jsonobjectrequest; import com.nytimes.nytimes.r; import com.nytimes.nytimes.adapters.postlistrecyclerviewadapter; import com.nytimes.nytimes.app.appcontroller; import com.nytimes.nytimes.pojos.postdata; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.util.arraylist; import java.util.list; public class postlistactivity extends appcompatactivity { recyclerview poslistrecyclerview; private list<postdata> postdatalist = new arraylist<postdata>(); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_post_list); setuptoolbar(); //=============== hit sendjsonresquest ===================// sendjsonrequest(); //=============== initialize recyclerview ===================// poslistrecyclerview = (recyclerview) findviewbyid(r.id.recyclerpostlist); poslistrecyclerview.sethasfixedsize(true); poslistrecyclerview.setlayoutmanager(new linearlayoutmanager(postlistactivity.this)); } //=================== setuptoolbar =====================// private void setuptoolbar() { toolbar postlisttoolbar = (toolbar) findviewbyid(r.id.postlisttoolbar); setsupportactionbar(postlisttoolbar); getsupportactionbar().setdisplayshowtitleenabled(false); getsupportactionbar().sethomeasupindicator(r.mipmap.ic_left); getsupportactionbar().setdisplayhomeasupenabled(true); } //=================== send json request =====================// private void sendjsonrequest() { //getintent category intent intentgetcategory = getintent(); string category = intentgetcategory.getextras().getstring("category"); //find specific url string format string turl = string.format("http://api.nytimes.com/svc/topstories/v1/%s.json?api-key=626663e02b8fe9d8:7:74012032", category); jsonobjectrequest request = new jsonobjectrequest(request.method.get, turl, (string) null, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { //hit parsejsonresponse parsejsonresponse(response); //setadapter recyclerview poslistrecyclerview.setadapter(new postlistrecyclerviewadapter(postlistactivity.this, postdatalist)); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { toast.maketext(postlistactivity.this, "onerrorresponse volley error listener", toast.length_short).show(); } }); //add request request queue appcontroller.getinstance().addtorequestqueue(request); } //=================== parsejsonresponse =====================// private void parsejsonresponse(jsonobject response) { if (response != null) { try { jsonarray results = response.getjsonarray("results"); (int = 0; < results.length(); i++) { jsonobject currentresult = results.getjsonobject(i); postdata postdata = new postdata(); postdata.setposttitle(currentresult.getstring("title")); postdata.setposturl(currentresult.getstring("url")); jsonarray multimedias = currentresult.getjsonarray("multimedia"); (int m = 0; m < multimedias.length(); m++) { jsonobject thumbnail = multimedias.getjsonobject(0); postdata.setpostthumbnail(thumbnail.getstring("url")); } postdatalist.add(postdata); } } catch (jsonexception e) { e.printstacktrace(); } } else { toast.maketext(postlistactivity.this, "response empty else parsejsonresponse", toast.length_short).show(); } } }
i think adapter isn't in place .
you initialize adapter first time when download json data , until recyclerview
doesn't have adapter work with.
i suggest:
1) define adapter , data array @ class level (outside oncreate):
postlistrecyclerviewadapter adapter; arraylist<postdata> postdatalist;
2) initialize empty adapter once in oncreate
@ time of initializing recyclerview
empty postdatalist
.
//=============== initialize recyclerview ===================// poslistrecyclerview = (recyclerview) findviewbyid(r.id.recyclerpostlist); poslistrecyclerview.sethasfixedsize(true); poslistrecyclerview.setlayoutmanager(new linearlayoutmanager(postlistactivity.this)); //=============== initialize adapter, data array , set adapter ===================// postdatalist = new arraylist<postdata>(); postlistrecyclerviewadapter adapter = new postlistrecyclerviewadapter(this, postdatalist) poslistrecyclerview.setadapter(adapter);
3) update postdatalist
when download new data , insert in postdatalist
4) call notifydatasetchanged
on adapter notify data has changed.
where setting adapter in onresponse
, call following function after have inserted new data postdatalist
.
adapter.notifydatasetchanged();
Comments
Post a Comment