How to connect a salvo to recyclerview?

I am trying to take data from the internet and with a salvo and display it in Recylerview.

I can easily display data in toast, but in case of recylerview, I have a problem. I follow Slidenerd from youtube:

https://www.youtube.com/playlist?list=PLonJJ3BVjZW6CtAMbJz1XD8ELUs1KXaTD

but it doesn't show up in Recyclerview, any 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);
            for (int i = 0; i < 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 the layout for this 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;
}

      

+3


source to share





All Articles