This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
With a ViewHolder in place, the rest of the Adapter Class can be completed.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
With our view holder configured and
leveraging the power of the Android data
0:00
binding library, we need to turn our
attention to the rest of the adapter.
0:04
Recall that this view holder allows for
smooth scrolling in our recycler view.
0:08
We'll fill in the adapter so
that it can convert an object
0:13
at a given position in our
dataset to a list row item.
0:16
We saw that our adapter requires
three different methods.
0:20
onCreateViewHolder(), which is called
right when the adapter is created, and
0:24
is used to initialize the ViewHolder.
0:29
onBindViewHolder(), which is called for
each ViewHolder to bind it to the adapter.
0:31
This is where data is
passed to the ViewHolder.
0:37
And getItemCount(),
0:40
which simply returns the size of
the collection the items to display.
0:42
It is used by the layout manager.
0:46
We also need to resolve those errors.
0:49
That's a bit of work to get done, but if
we take it step by step, we can do this.
0:51
So let's get started.
0:56
To start with,
we need some variables defined.
0:58
Let's give some space.
1:02
We need a private List<Hour> hours.
1:06
Do the quick fix to get our imports.
1:12
And we need our context.
1:16
Context context.
1:19
And we'll generate a constructor here for
our hourly adapter.
1:22
Code > Generate > Constructor.
1:28
Select both variables, and hit OK.
1:32
We'll be using that over in
the activity here in a little while.
1:37
In onCreateViewHolder(),
1:40
we want to bind our data to the view
holder when it's first created.
1:42
Let's update the return type from
a RecyclerView.ViewHolder to
1:46
our hourly adapter view holder.
1:51
Go here.
1:54
We're gonna change that to HourlyAdapter.
1:55
Let's scroll up here to
give us some working room.
2:04
Inside the onCreateViewHolder method,
we create our binding.
2:12
So HourlyListItemBinding.
2:18
We will call it binding.
2:21
DataBindingUtil.
2:24
So we can chain on our inflate method
here, just like we do in main activity,
2:28
and use the layout inflater.
2:32
Let's drop this down to a new line.
2:34
We'll inflate.
2:38
Layout inflater.
2:39
We want parent.
2:41
And getContext.
2:43
The next parameter here is our layout.
2:46
We drop that down.
2:49
R.layout.
2:51
It's the hourly_list_item.
2:54
And use the quick fix to import R.
3:01
And our ViewGroup is parent And we don't
want this attached to to the parent,
3:05
so we can pass in false
Let's clean this up here.
3:14
Drop this down to a new line.
3:20
Recall that inflating in this manner
inflates the binding layout and
3:23
returns the newly created binding for
that layout.
3:27
Now, we just need to return our view
holder and pass in the binding.
3:32
so we will return a new ViewHolder(),
and pass in our binding.
3:36
Okay, onto onBindViewHolder,
where we aren't passing
3:44
any recyclerView.ViewHolder,
just a ViewHolder.
3:48
So we can delete that.
3:51
Inside onBindViewHolder,
3:59
we need to find which list item in
our hourly data we're working with.
4:01
So Hour hour = hours.get(position).
4:06
And we bind that to our ViewHolder.
4:14
So there's our view holder.
4:18
hourlyListItemBinding.setHour(hour).
4:19
And that's it for that method.
4:27
And then, getItemCount, we just need
to return the size of our data.
4:28
So we can return hours.size().
4:35
We should be all set with our adapter and
ViewHolder.
4:40
Next up, we'll head into our
hourly forecast activity, and
4:43
connect the pieces together in there.
4:47
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up