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
Let's create an Adapter!
Additional Resources
Android Resources
- Android Adapter
- Android RecyclerView.ViewHolder
Java Resources
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 good start and what will be our
user interface, we now need to create
0:00
our adapter class and ViewHolder subclass
to connect our data to our display.
0:04
Since the adapter relies on the
ViewHolder, we'll tackle that code first.
0:09
The ViewHolder is a subclass
that resides inside our adapter.
0:13
Let's get that adapter class all set up.
0:18
To keep a clean code base,
let's make a new package called adapters.
0:20
New package, Adapters.
0:24
And inside here, do a new Java class.
0:30
We'll name it HourlyAdapter.
0:35
And the superclass, it's gonna
come from the RecycleView.Adapter.
0:38
And hit OK.
0:45
And first thing, we get an error.
0:48
We hover over the error,
it shows about how this class
0:51
must either be declared abstract or
implement the abstract method listed.
0:55
If we do the quick fix,
there's an option to implement methods.
0:59
Implement methods, we want all of those,
so we'll select them all and hit OK.
1:03
And with the magic of Android Studio,
our methods are set up and
1:07
our errors are gone.
1:11
We see that Android Studio has added
some annotations in here for us as well.
1:13
We're overriding some and
we have this NonNull annotation too.
1:17
If that one is unfamiliar to you, it
allows the compiler to determine cases where
1:22
a code path might receive a null value.
1:27
And it helps to prevent
NullPointerExceptions.
1:30
Check the teacher's notes for
more information on NonNull annotations.
1:33
We need to write our subclass for
1:37
our ViewHolder which will describe
the views within each row item.
1:39
Let's come down here to the bottom.
1:44
We'll give some space.
1:46
We'll do public class ViewHolder and
1:50
we want it to extend from
RecyclerView.ViewHolder.
1:56
And we see, there's another error.
2:06
This error says there is no
default constructors available.
2:08
Now, we'll fix that here in a second.
2:12
But with our ViewHolder subclass defined,
2:14
we can specify it up in our
adapter class definition.
2:16
We're gonna use HourlyAdapter.ViewHolder.
2:22
All right.
2:33
Let's head back to our sub class
where there's a few things to add.
2:34
We want our binding variables,
and we want our constructor.
2:40
Our constructor will do view lookups for
each subview.
2:51
Okay.
3:00
Let's start with the binding.
3:01
Recall that by using the Android data
binding library, it generates a class for
3:03
us based on the layout name.
3:07
By default, the name of the class is
based on the name of the layout file,
3:10
converts it to pascal case, and
adds the binding suffix to it.
3:14
In this case, it is HourlyListItemBinding.
3:19
So we'll do public HourlyListItemBinding,
3:24
and it cannot resolve that symbol.
3:35
Let's see.
3:36
We have a typo here in our name for
list_item.xml.
3:38
Let's refactor and rename that.
3:41
Clean that up.
3:48
Okay.
So sometimes,
3:57
Android Studio doesn't do our import
of our binding for us automatically.
3:59
We'll do a quick fix to do our import.
4:05
And now, for our constructor.
4:08
So we want public ViewHolder.
4:12
It will take an HourlyListItemBinding
as our parameter.
4:19
HourlyLayoutBinding is
what we'll call that.
4:28
So inside the constructor,
we call super in passing our binding.
4:34
So super(hourlyLayoutBinding).
4:38
And then, we also need a call to getRoot,
which returns the outermost view in
4:43
the layout file associated
with our particular binding.
4:48
And then we connect our bindings.
4:55
hourlyListItemBinding =
hourlyLayoutBinding.
4:59
That was kinda of a lot.
5:07
Let's go over that one more time.
5:08
Our ViewHolder extends from
RecyclerView.ViewHolder, and
5:11
we bind the variables that
are being used inside the view.
5:15
Then, we create a constructor for
5:18
this ViewHolder class to do
view lookups for each subview.
5:20
The constructor takes an item binding as
the parameter, which we pass into super
5:24
inside the constructor and
call getRoot to grab the outermost view.
5:29
Then, we connect the bindings together.
5:34
And that's it for the ViewHolder.
5:37
Let's take a quick break to get up and
stretch a bit,
5:39
and then get the rest
of the adapter set up.
5:42
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