RecyclerView and CardView in Android

Duration

15 minutes

Tip: If you are doing this exercise live in a session, make sure to make good use of the instructor, they are online to answer any questions you have!

Goals

In this exercise, you will implement an adapter. The adapter is responsible for inflating your layout file, instantiating your view holder, and binding data from your data set into the UI.

Required assets

The provided Resources folder for this part contains a subfolder named Completed with a solution you can use to check your work. This lab is a continuation of the previous one. If you did not complete the previous exercise, you can use the Completed solution from the previous part as starter code for this part.

Challenge

Use the guidelines here to code an adapter for your restaurant data. Alternatively, you can use the step-by-step instructions given below.

  1. Create a class named RestaurantAdapter. Be sure to derive it from the required base.
  2. Add a constructor that takes your sample restaurant data as a parameter and stores it in a field.
  3. Override the ItemCount property: return the number of items in your data set.
  4. Override the OnCreateViewHolder method: inflate a layout file and instantiate a view holder.
  5. Override the OnBindViewHolder method: load the new data into your views.
  6. Load an instance of your adapter into your RecyclerView.

Steps

Below are the step-by-step instructions to implement the exercise.

Code an adapter

  1. Add a new class named RestaurantAdapter to your project.
  2. Derive your adapter from RecyclerView.Adapter.
  3. Your adapter will need access to your data set. Typically, this is passed as a parameter to the constructor and then stored in a field. Implement your constructor this way: it should take a List<Restaurant> as its parameter and store it in a field of that same type.
  4. Override the ItemCount property so it returns the number of items in your data set. You have the list of restaurants stored in a field so you can return the Count property from the list.
  5. Override the OnCreateViewHolder method. Inflate a layout file and instantiate a view holder. This takes two steps: first inflate your Restaurant.axml layout file, then create a view holder passing the inflated layout file to the view-holder constructor. Return the view holder as your result. The implementation is given below if you need it, hidden behind a button.

    Show Code

  6. Override the OnBindViewHolder method. There are two steps here: cast the passed view-holder parameter to your view-holder type. Use the view holder to load the new data at the given position into your views. The implementation is given below if you need it, hidden behind a button.

    Show Code

Use your adapter

  1. Open MainActivity.cs.
  2. In your OnCreate method, create an instance of your adapter class. Use the SampleData.GetRestaurants() method to get the data set you will need to pass to your adapter's constructor.
  3. Use the RecyclerView's SetAdapter method to load your adapter instance into your RecyclerView.
  4. Run the app to test your work. You should see the name and rating displayed for each restaurant.

Summary

Here you implemented an adapter for your restaurant data. As the name implies, your adapter serves as a bridge between your data and the UI displayed inside a RecyclerView. Your adapter does three things for the RecyclerView: creates UI for a data item, updates the UI when layouts are recycled, and reports how many items are in your data set.

Go Back