Populating a RecyclerView with Volley response data

Nabeel
4 min readJan 24, 2021

--

In this article we will show you how to fill a RecyclerView with the response data you would get from Volley after making a GET request.

This is an extension to my previous tutorial on how to make GET/POST requests using Volley. If you don’t know how to use Volley, check out my previous article here:

Alternatively if you just want the code, I created a sample app for this project that you can find on GitHub here:

POJO Class

For this tutorial we will once again be using https://reqres.in/ which is a free site filled with fake data that you can use to test your GET requests against. More specifically we will be using the List Users URL here: https://reqres.in/api/users?page=1. If you take a look at the JSON, in the “data” array, you will see each object contains an id, email, first name and last name value (We will be ignoring the avatar field). We will have to combine these fields into one object that will then be placed into an ArrayList. So, the first step is to create a standard Plain Old Java Object, or POJO, class that will aggregate all of our values:

This class contains an integer id field and String email, first name and last name fields. Then we have our getters and setters and we override the toString method, which isn’t necessary but it always recommended to do.

RecyclerView

Next we need to set up our RecyclerView. If you are new to RecyclerViews or are coming from ListViews I recommend reading the RecyclerView docs here: https://developer.android.com/guide/topics/ui/layout/recyclerview. It’s pretty easy to set up. RecyclerViews are more efficient and customizable than ListViews.

The first step will be to create a layout for each individual RecyclerView element. This layout will define how our data looks in the list. This layout will have to contain TextViews for each of our fields (id, email, first name and last name) that we retrieve. Here is a basic layout:

The layout contains four TextViews, one for each value that we are retrieving.

Next, we will need to create an Activity and layout to add our RecyclerView to. For the sample app for this tutorial I created a simple layout with a RecyclerView and a button that will retrieve our data when pressed:

Then, we will need to create an Adapter and ViewHolder. The adapter will need to accept a List of type DataPOJO and the ViewHolder will contain the methods for setting each TextView:

The adapter constructor takes in two parameters: a List of DataPOJO objects and a Context. The Context is needed because this specific Adapter implementation is in a separate normal Java class and the LayoutInflater in the onCreateViewHolder method needs a context passed into it. Then in the onBindViewHolder method we call the setters for the TextViews from the ViewHolder and pass in the corresponding value from our retrievedResponses ArrayList.

GET Request

Make sure you have added the Volley Dependency in your app level Gradle and Internet permission in your Manifest.

First we will create a basic Activity:

Here we simply initialize the RecyclerView and Button we created in the activity_main layout. Then we set the LayoutManager for the RecyclerView which is just going to be a LinearLayoutManager (You can read about other LayoutManagers that let you display your data in different ways here: https://developer.android.com/guide/topics/ui/layout/recyclerview#plan-your-layout). Finally we set an onClickListener for our button that will call our volley GET request method.

Now we write up the GET request:

The request is extremely similar to the one we created in my last tutorial, however, there are some differences. Previously we created an ArrayList that only accepted Strings, now however, we created an ArrayList that accepts objects of type DataPOJO. Then in the onResponse method where we previously only retrieved one value from each object, we now retrieve four: the id, email, first_name and last_name. We then create a new DataPOJO object and add that value to the jsonResponses ArrayList. Finally, we call setAdapter on our response_recycler_view object and pass in a new RecyclerViewAdapter that contains the ArrayList argument and a context. We put the call to setAdapter inside the onResponse method so that whenever your response comes in, even if it takes a while, the RecyclerView will update with the data automatically.

Now when we run the app and tap on Retrieve Data our RecyclerView pops up with the data we got:

That’s all! You should have a pretty good grasp now on how to populate a RecyclerView with Volley response data. If you have any questions leave a response below and I’ll try to answer it.

--

--