json - How to connect volley to recyclerview? -
i trying take data web , of volley , display in recylerview.
i can display data in toast in case of recylerview having problem. have follow slidenerd youtube:
https://www.youtube.com/playlist?list=plonjj3bvjzw6ctambjz1xd8elus1kxatd
but not displaying in recyclerview, ideas?
fragmentcollege.java class:
public class fragmentcollege extends fragment { public static final string url_edusanjal_college = "http://edusanjal.com/api/edusanjal/college/?format=json"; private volleysingleton volleysingleton; private imageloader imageloader; private requestqueue requestqueue; private arraylist<college> listcollege = new arraylist<>(); private recyclerview listcollegehits; private adaptercollege madaptercollege; public fragmentcollege() { // required empty public constructor } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); volleysingleton = volleysingleton.getinstance(); requestqueue = volleysingleton.getrequestqueue(); } private void sendjsonrequest() { jsonobjectrequest request = new jsonobjectrequest(request.method.get, url_edusanjal_college, (string) null, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { listcollege = parsejsonresponse(response); madaptercollege.setcollegelist(listcollege); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { } }); requestqueue.add(request); } private arraylist<college> parsejsonresponse(jsonobject response) { arraylist<college> listcollege = new arraylist<>(); if (response == null || response.length() == 0) { try { stringbuilder data = new stringbuilder(); jsonarray arraycollege = response.getjsonarray(key_objects); (int = 0; < arraycollege.length(); i++) { jsonobject currentcollege = arraycollege.getjsonobject(i); long id = currentcollege.getlong(key_id); string title = currentcollege.getstring(key_title); string city = currentcollege.getstring(key_city); string phone = currentcollege.getstring(key_phone); data.append(id + "\n" + title + "\n" + city + "\t" + phone + "\n" + "\n"); college college = new college(); college.setid(id); college.settitle(title); college.setcity(city); college.setphone(phone); listcollege.add(college); } } catch (jsonexception e) { } l.t(getactivity(), listcollege.tostring()); } return listcollege; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_college, container, false); listcollegehits = (recyclerview) view.findviewbyid(r.id.listcollegehits); listcollegehits.setlayoutmanager(new linearlayoutmanager(getactivity())); madaptercollege = new adaptercollege(getactivity()); listcollegehits.setadapter(madaptercollege); sendjsonrequest(); // inflate layout fragment return view; } }
fragemnt_college.xml:
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.edusanjal.edusanjal.activity.collegefragment"> <!-- todo: update blank fragment layout --> <android.support.v7.widget.recyclerview android:id="@+id/listcollegehits" android:layout_width="match_parent" android:layout_height="match_parent" /> <textview android:id="@+id/textvolleyerror" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:padding="@dimen/size_word" android:text="#ff4444" android:visibility="gone"/>
custom_college_layout.xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginbottom="@dimen/size_byte" android:layout_marginleft="@dimen/size_word" android:layout_marginright="@dimen/size_word" android:layout_margintop="@dimen/size_byte"> <imageview android:id="@+id/collegelogo" android:layout_width="@dimen/college_thumbnail_width" android:layout_height="@dimen/college_thumbnail_height" android:layout_centervertical="true" android:src="@mipmap/ic_launcher"/> <textview android:id="@+id/collegetitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_aligntop="@+id/collegelogo" android:layout_marginleft="56dp" android:alpha="0.87" android:gravity="right" android:padding="@dimen/size_half_byte" android:text="aadikabi bhanubhakta higher secondary school" android:textsize="@dimen/size_text_primary"/> <textview android:id="@+id/collegephone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_below="@+id/collegetitle" android:layout_marginleft="56dp" android:alpha="0.87" android:gravity="right" android:padding="@dimen/size_half_byte" android:text="phone" android:textsize="@dimen/size_text_primary"/> <textview android:id="@+id/collegecity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_below="@+id/collegephone" android:layout_marginleft="56dp" android:alpha="0.87" android:gravity="right" android:padding="@dimen/size_half_byte" android:text="city" android:textsize="@dimen/size_text_primary"/> <textview android:id="@+id/collegecreatedon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_below="@id/collegecity" android:alpha="0.87" android:padding="@dimen/size_half_byte" android:text="31-31-31" android:textsize="@dimen/size_text_secondary"/>
adaptercollege.java:
public class adaptercollege extends recyclerview.adapter<adaptercollege.viewholdercollege> { private arraylist<college> listcollege = new arraylist<>(); private layoutinflater mlayoutinflater; private volleysingleton volleysingleton; private imageloader imageloader; private dateformat dateformatter=new simpledateformat("yyyy-mm-dd"); public adaptercollege(context context) { mlayoutinflater = mlayoutinflater.from(context); volleysingleton = volleysingleton.getinstance(); imageloader = volleysingleton.getimageloader(); } public void setcollegelist(arraylist<college> listcollege) { this.listcollege = listcollege; notifyitemrangechanged(0, listcollege.size()); } @override public viewholdercollege oncreateviewholder(viewgroup parent, int viewtype) { view view = mlayoutinflater.inflate(r.layout.custom_college_layout, parent, false); viewholdercollege viewholder = new viewholdercollege(view); return viewholder; } @override public void onbindviewholder(viewholdercollege holder, int position) { college currentcollege = listcollege.get(position); holder.collegetitle.settext(currentcollege.gettitle()); date collegecreatedon=currentcollege.getcreatedon(); if(collegecreatedon!=null){ string formatteddate=dateformatter.format(collegecreatedon); holder.collegecreatedon.settext(formatteddate); } else{ holder.collegecreatedon.settext(constants.na); } holder.collegecity.settext(currentcollege.getcity()); holder.collegephone.settext(currentcollege.getphone()); string urllogo = currentcollege.geturllogo(); loadimages(urllogo, holder); } private void loadimages(string urllogo, final viewholdercollege holder) { if (!urllogo.equals(constants.na)) { imageloader.get(urllogo, new imageloader.imagelistener() { @override public void onresponse(imageloader.imagecontainer response, boolean isimmediate) { holder.collegelogo.setimagebitmap(response.getbitmap()); } @override public void onerrorresponse(volleyerror error) { } }); } } @override public int getitemcount() { return listcollege.size(); } static class viewholdercollege extends recyclerview.viewholder { imageview collegelogo; textview collegetitle; textview collegecreatedon; textview collegephone; textview collegecity; public viewholdercollege(view itemview) { super(itemview); collegelogo = (imageview) itemview.findviewbyid(r.id.collegelogo); collegetitle = (textview) itemview.findviewbyid(r.id.collegetitle); collegecreatedon = (textview) itemview.findviewbyid(r.id.collegecreatedon); collegephone = (textview) itemview.findviewbyid(r.id.collegephone); collegecity = (textview) itemview.findviewbyid(r.id.collegecity); } }
pojo class => college.java:
public class college { private long id; private string title; private date createdon; private string phone; private string city; private string urllogo; public college(){ } public college(long id, string title, date createdon, string phone, string city, string urllogo){ this.id=id; this.title=title; this.createdon=createdon; this.phone=phone; this.city=city; this.urllogo=urllogo; } public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public long getid() { return id; } public void setid(long id) { this.id = id; } public date getcreatedon() { return createdon; } public void setcreatedon(date createdon) { this.createdon = createdon; } public string getphone() { return phone; } public void setphone(string phone) { this.phone = phone; } public string getcity() { return city; } public void setcity(string city) { this.city = city; } public string geturllogo() { return urllogo; } public void seturllogo(string urllogo) { this.urllogo = urllogo; } @override public string tostring() { return "id: "+id+ "title "+title+ "date "+createdon+ "phone "+phone+ "city "+city+ "urllogo "+urllogo+ "\n"; } }
network class => volleysingleton.java:
public class volleysingleton { private static volleysingleton sinstance = null; private imageloader mimageloader; private requestqueue mrequestqueue; private volleysingleton() { mrequestqueue = volley.newrequestqueue(myapplication.getappcontext()); mimageloader = new imageloader(mrequestqueue, new imageloader.imagecache() { private lrucache<string, bitmap> cache = new lrucache<>((int) (runtime.getruntime().maxmemory() / 1024) / 8); @override public bitmap getbitmap(string url) { return cache.get(url); } @override public void putbitmap(string url, bitmap bitmap) { cache.put(url, bitmap); } }); } public static volleysingleton getinstance() { if (sinstance == null) { sinstance = new volleysingleton(); } return sinstance; } public requestqueue getrequestqueue() { return mrequestqueue; } public imageloader getimageloader(){ return mimageloader; }
Comments
Post a Comment