java - JsonObject Can't Converted to jsonArray -
always showing warning is
e/recyclerview: no adapter attached; skipping layout
when trying implement recycler view inside fragment , showing error
w/system.err: com.android.volley.parseerror: org.json.jsonexception: value {"sales_report":[{"id":"1","cash":"258","credit":"258","description":"","dates":"nov-09-2016","camimg_path":""},{"id":"2","cash":"532","credit":"586","description":"","dates":"nov-09-2016","camimg_path":"gallery_images\/test_gl.jpeg"},{"id":"3","cash":"214","credit":"980","description":"th","dates":"nov-09-2016","camimg_path":"gallery_images\/test_gl.jpeg"},{"id":"4","cash":"123","credit":"321","description":"erp","dates":"nov-09-2016","camimg_path":"gallery_images\/test_gl.jpeg"},{"id":"15","cash":"5","credit":"8","description":"5","dates":"nov-09-2016","camimg_path":"gallery_images\/galimg2016-11-09t14:44:21.483+05302516.jpeg"},{"id":"14","cash":"2","credit":"3","description":"","dates":"nov-09-2016","camimg_path":"..\/gallery_images\/galimg2016-11-09t14:42:56.857+05302620.jpeg"}]} of type org.json.jsonobject cannot converted jsonarray w/system.err: @ com.android.volley.toolbox.jsonarrayrequest.parsenetworkresponse(jsonarrayrequest.java:55) w/system.err: @ com.android.volley.networkdispatcher.run(networkdispatcher.java:121) w/system.err: caused by: org.json.jsonexception: value {"sales_report":[{"id":"1","cash":"258","credit":"258","description":"","dates":"nov-09-2016","camimg_path":""},{"id":"2","cash":"532","credit":"586","description":"","dates":"nov-09-2016","camimg_path":"gallery_images\/test_gl.jpeg"},{"id":"3","cash":"214","credit":"980","description":"th","dates":"nov-09- 2016","camimg_path":"gallery_images\/test_gl.jpeg"}, {"id":"4","cash":"123","credit":"321","description":"erp","dates":"nov-09- 2016","camimg_path":"gallery_images\/test_gl.jpeg"}, {"id":"15","cash":"5","credit":"8","description":"5","dates":"nov-09- 2016","camimg_path":"gallery_images\/galimg2016-11- 09t14:44:21.483+05302516.jpeg"}, {"id":"14","cash":"2","credit":"3","description":"","dates":"nov-09- 2016","camimg_path":"..\/gallery_images\/galimg2016-11- 09t14:42:56.857+05302620.jpeg"}]} of type org.json.jsonobject cannot converted jsonarray w/system.err: @ org.json.json.typemismatch(json.java:111) w/system.err: @ org.json.jsonarray.<init>(jsonarray.java:96) w/system.err: @ org.json.jsonarray.<init>(jsonarray.java:108) /system.err: @ com.android.volley.toolbox.jsonarrayrequest.parsenetworkresponse(jsonarrayreques t.java:50)
help me.... fragment.java
public class messagefragment extends fragment { private list<content> listsuperheroes; //creating views private recyclerview recyclerview; private recyclerview.layoutmanager layoutmanager; private recyclerview.adapter adapter; public messagefragment() { // required empty public constructor } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view v= inflater.inflate(r.layout.fragment_message, container, false); recyclerview = (recyclerview) v.findviewbyid(r.id.recyclerview); recyclerview.sethasfixedsize(true); layoutmanager = new linearlayoutmanager(getactivity()); recyclerview.setlayoutmanager(layoutmanager); //adding adapter recyclerview listsuperheroes = new arraylist<>(); //calling method data getdata(); return v; } private void getdata(){ //showing progress dialog // final progressdialog loading = progressdialog.show(getactivity(),"loading data", "please wait...",false,false); //creating json array request jsonarrayrequest jsonarrayrequest = new jsonarrayrequest(config.data_url, new response.listener<jsonarray>() { @override public void onresponse(jsonarray response) { //dismissing progress dialog // loading.dismiss(); //calling method parse json array parsedata(response); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { } }); //creating request queue requestqueue requestqueue = volley.newrequestqueue(getactivity().getapplicationcontext()); //adding request queue requestqueue.add(jsonarrayrequest); } private void parsedata(jsonarray array){ for(int = 0; i<array.length(); i++) { content superhero = new content(); jsonobject json = null; try { json = array.getjsonobject(i); superhero.setimageurl(json.getstring(config.tag_image_url)); superhero.setname(json.getstring(config.tag_name)); superhero.setrank(json.getint(config.tag_rank)); superhero.setrealname(json.getstring(config.tag_real_name)); superhero.setcreatedby(json.getstring(config.tag_created_by)); superhero.setfirstappearance(json.getstring(config.tag_first_appearance)); arraylist<string> powers = new arraylist<string>(); jsonarray jsonarray = json.getjsonarray(config.tag_powers); for(int j = 0; j<jsonarray.length(); j++){ powers.add(((string) jsonarray.get(j))+"\n"); } superhero.setpowers(powers); } catch (jsonexception e) { e.printstacktrace(); } listsuperheroes.add(superhero); } adapter = new cardadapter(listsuperheroes, getactivity()); //finally initializing our adapter recyclerview.setadapter(adapter); } }
and here cardadapter.java classs
public class cardadapter extends recyclerview.adapter<cardadapter.viewholder> { private imageloader imageloader; private context context; //list of superheroes list<content> superheroes; public cardadapter(list<content> superheroes, context context){ super(); //getting superheroes this.superheroes = superheroes; this.context = context; } @override public viewholder oncreateviewholder(viewgroup parent, int viewtype) { view v = layoutinflater.from(parent.getcontext()) .inflate(r.layout.content_list, parent, false); viewholder viewholder = new viewholder(v); return viewholder; } @override public void onbindviewholder(viewholder holder, int position) { content superhero = superheroes.get(position); imageloader = customvolleyrequest.getinstance(context).getimageloader(); imageloader.get(superhero.getimageurl(), imageloader.getimagelistener(holder.imageview, r.mipmap.ic_launcher, android.r.drawable.ic_dialog_alert)); holder.imageview.setimageurl(superhero.getimageurl(), imageloader); holder.textviewname.settext(superhero.getname()); holder.textviewrank.settext(string.valueof(superhero.getrank())); holder.textviewrealname.settext(superhero.getrealname()); holder.textviewcreatedby.settext(superhero.getcreatedby()); holder.textviewfirstappearance.settext(superhero.getfirstappearance()); string powers = ""; for(int = 0; i<superhero.getpowers().size(); i++){ powers+= superhero.getpowers().get(i); } holder.textviewpowers.settext(powers); } @override public int getitemcount() { return superheroes.size(); } class viewholder extends recyclerview.viewholder{ public networkimageview imageview; public textview textviewname; public textview textviewrank; public textview textviewrealname; public textview textviewcreatedby; public textview textviewfirstappearance; public textview textviewpowers; public viewholder(view itemview) { super(itemview); imageview = (networkimageview) itemview.findviewbyid(r.id.imageviewhero); textviewname = (textview) itemview.findviewbyid(r.id.textviewname); textviewrank= (textview) itemview.findviewbyid(r.id.textviewrank); textviewrealname= (textview) itemview.findviewbyid(r.id.textviewrealname); textviewcreatedby= (textview) itemview.findviewbyid(r.id.textviewcreatedby); textviewfirstappearance= (textview) itemview.findviewbyid(r.id.textviewfirstappearance); textviewpowers= (textview) itemview.findviewbyid(r.id.textviewpowers); } } }
please me find problem in code , how solve it... in advance.
you getting error because while recyclerview
visible
has no adapter.
currently setting adapter
in fragment
's parsedata()
method. afaik there no drawback not setting adapter
(just gives out warning error). if don't want add adapter in oncreateview()
so
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view v= inflater.inflate(r.layout.fragment_message, container, false); recyclerview = (recyclerview) v.findviewbyid(r.id.recyclerview); // code listsuperheroes = new arraylist<>(); adapter = new cardadapter(listsuperheroes, getactivity()); // set adapter here don't warning. recyclerview.setadapter(adapter); // code return v; }
and in parsedata()
method notify adapter
data has changed.
private void parsedata(jsonarray array) { // code parsing // notify adapter data has changed , adapter populate recyclerview accordingly. adapter.notifydatasetchanged(); }
Comments
Post a Comment