Duration
15 minutes
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.
- Create a class named
RestaurantAdapter
. Be sure to derive it from the required base. - Add a constructor that takes your sample restaurant data as a parameter and stores it in a field.
- Override the
ItemCount
property: return the number of items in your data set. - Override the
OnCreateViewHolder
method: inflate a layout file and instantiate a view holder. - Override the
OnBindViewHolder
method: load the new data into your views. - Load an instance of your adapter into your
RecyclerView
.
Steps
Below are the step-by-step instructions to implement the exercise.
Code an adapter
-
Add a new class named
RestaurantAdapter
to your project. -
Derive your adapter from
RecyclerView.Adapter
. -
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. -
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 theCount
property from the list. -
Override the
OnCreateViewHolder
method. Inflate a layout file and instantiate a view holder. This takes two steps: first inflate yourRestaurant.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. -
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.
Use your adapter
- Open MainActivity.cs.
-
In your
OnCreate
method, create an instance of your adapter class. Use theSampleData.GetRestaurants()
method to get the data set you will need to pass to your adapter's constructor. -
Use the
RecyclerView
'sSetAdapter
method to load your adapter instance into yourRecyclerView
. - 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.