android - No RecyclerView after JSON Parse -
i'm trying populate recyclerview
json data local server using volley
, recyclerview
doesn't display , no error displayed in logcat.
here code:
mainactivity.java
list<products> productslist; recyclerview recyclerview; ... @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar = (toolbar) findviewbyid(r.id.toolbar); toolbar2 = (toolbar) findviewbyid(r.id.toolbar2); setsupportactionbar(toolbar2); recyclerview = (recyclerview) findviewbyid(r.id.main_list); recyclerview.setlayoutmanager(new linearlayoutmanager(this)); recyclerview.setitemanimator(new defaultitemanimator()); final dataadapter dataadapter = new dataadapter(productslist); recyclerview.setadapter(dataadapter); requestqueue requestqueue = volley.newrequestqueue(getapplicationcontext()); string url = "http://192.168.9.120/menproducts.json"; jsonarrayrequest jsonarrayrequest = new jsonarrayrequest(url, new response.listener<jsonarray>() { @override public void onresponse(jsonarray response) { try { if (response.length() > 0) { productslist.clear(); (int = 0; < response.length(); i++) { jsonobject jsonobject = response.getjsonobject(i); products products = new products(); if (!jsonobject.isnull("name")) { products.name = jsonobject.getstring("name"); } if (!jsonobject.isnull("age")) { products.departments = jsonobject.getstring("age"); } productslist.add(i, products); } dataadapter.notifydatasetchanged(); } } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { system.out.println(error.getmessage()); toast.maketext(getapplicationcontext(), error.getmessage(), toast.length_long).show(); } }); requestqueue.add(jsonarrayrequest); }
dataadapter.java
public class dataadapter extends recyclerview.adapter<dataadapter.productsviewholder> { private list<products> products; public dataadapter(list<products> products){ this.products = products; } @override public productsviewholder oncreateviewholder(viewgroup viewgroup, int i){ view view = layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.tile_view, viewgroup, false); productsviewholder pvh = new productsviewholder(view); return pvh; } @override public void onbindviewholder(productsviewholder viewholder, int i){ viewholder.item_title.settext(products.get(i).gettitle()); viewholder.item_desc.settext(string.valueof(products.get(i).getdepartments())); } @override public int getitemcount(){ if(products != null){ return products.size(); } return 0; } @override public void onattachedtorecyclerview(recyclerview recyclerview){ super.onattachedtorecyclerview(recyclerview); } public static class productsviewholder extends recyclerview.viewholder{ textview item_title; textview item_desc; cardview cv; productsviewholder(view view){ super(view); cv = (cardview) view.findviewbyid(r.id.note_item); item_title = (textview) view.findviewbyid(r.id.item_title); item_desc = (textview) view.findviewbyid(r.id.item_desc); } } }
products.java
public class products { public string name; public string departments; public string info; public float rate; public double price; public products(){ } public products(string name, string departments, string info, float rate, double price){ this.name = name; this.info = info; this.price = price; this.rate = rate; this.departments = departments; } public string gettitle(){ return name; } public void settitle(string name){ this.name = name; } public string getdepartments(){ return departments; } public void setdepartments(string departments){ this.departments = departments; } public string getinfo(){ return info; } public void setinfo(string info) { this.info = info; } public float getrate(){ return rate; } public void setrate(float rate) { this.rate = rate; } public double getprice(){ return price; } public void setprice(double price) { this.price = price; } }
this json file
mensproducts.json
{ "products": [ { "departments": "grooming & health", "category": [ { "name": "shave" }, { "name": "skin care" }, { "name": "hair care" }, { "name": "body care" }, { "name": "oral care" }, { "name": "cologne" }, { "name": "kits , sets" }, { "name": "luxury grooming" } ] }, { "departments": "clothing, shoes & jewelry", "category": [ { "name": "clothing" }, { "name": "shoes" }, { "name": "jewelry" }, { "name": "watches" }, { "name": "accessories" } ] }, { "departments": "clothing, shoes & jewelry", "category": [ { "name": "clothing" }, { "name": "shoes" }, { "name": "jewelry" }, { "name": "watches" }, { "name": "accessories" } ] }, ] }
your current getitemcount return null , if products contain data. do.
@override public int getitemcount(){ if(products== null){ return 0; } else { return products.size(); } }