In this video we'll create the adaptor
for our RecyclerView so that it displays the tasks
in the database.
RecyclerView displays views as we saw
in the Flickr Browser app
and we need an adapter that sits
between the RecyclerView and the data we want to display.
Google had produced a cursor adaptor class
that provides data from a cursor to a list view
but they haven't provided a version
that works with the RecyclerView.
Well that's not the end of the world
because it's not really that difficult
to extend the RecyclerView.AdapterClass
to work with a cursor.
In the Flickr Browser app the adaptor used a list
to store the photo details.
What we'll do is create a cursor RecyclerView adaptor
along the same lines or on similar lines
but the data will come from a cursor instead of a list.
We're gonna start by creating a new Katlin class.
So let's go ahead and do that.
Obviously you're aware of this I would think by now.
Alright so you're gonna right click on tasktimer, new,
Katlin File/Class and we're gonna call
this one the CursorRecyclerViewAdapter.
We're gonna take the opportunity
to select Class on a kind just
to select or to save rather a little bit of time.
Alright, so we want this class
to extend the RecyclerView.AdapterClass
so that we can use it as an adapter for RecyclerView.
And it will be returning task view holder object.
So let's go ahead and do that first.
So we got a colon there to extend
and then on the next line we'll indent it a little bit.
It's gonna be RecyclerView.Adapter
and I'll just except that import.
And it's as I mentioned we're going
to be returning task view object.
So in the less than greater than sign
in other words the don't have to write up everything there.
I'm gonna type in, we'll type, TaskViewHolder.
And we need to add parenthesis after the greater than sign
and then left and right curly braces for the class.
I'm just gonna delete the extra two that were sitting there.
Alright so I'm gonna add our constant which is for our tag.
So it's going to be private const val
and it's gonna be TAG = double quotes CursorRecyclerView,
I'm not gonna type the whole word just Adapt.
And we're doing it to be careful with the log in TAG
because if you make the TAG string longer
than 23 characters things can go wrong.
So that's why I've truncated it.
And in reality if you're doing this
you're probably shortening entire class name
to something along the lines cursor of the adapter.
But then I wouldn't have had an excuse to remind you
about the propers with the long log tags.
Alright so we've got an error
because we haven't written the methods we need yet
but even when we have we'll get another error
because the task view holder class
of course doesn't exist yet.
So let's start by creating that.
Then we can forget about it for now anyway.
So we're going to do that basically
above the const definition there.
So I'm gonna come up here and start this on line 10.
So it's gonna be class and it's TaskViewHolder
and we want parenthesis val containerView:
and I can type in View because that's what it is.
It's a view, except the import.
Then we need to extend, so colon.
Now on the next line just indent it a bit
so that it's more readable
and it's gonna be RecyclerView.ViewHolder this time.
What we're extending and in parenthesis
it's going to be our containerView
which of course is the argument.
And we want left and right curly braces.
And in terms of the code,
we're gonna type in there is var name: Textview =
and it's going to be containerView
and I miss typed Textview there so I change that to TextView
with a capital V of course is the right one.
And it's containerView.findViewByID
and it's gonna be R.id.tli_name, okay.
I'll just delete that extra line there.
Now hang on a minute what's this findViewByID in Katlin.
Well yes, unfortunately the Katlin extensions only work
by default in activity fragment and view classes.
The obvious thing to do here is
to add the description property
and the two buttons calling findViewByID for each one.
That would work but frankly it's boring.
You already know how to do that.
We did it in the Flickr Browser app
and it's pretty standard stuff.
And most of the examples you find
on the internet will also be doing it that way.
So what I'm going to do is take this opportunity
to show you a more Katlin like way of doing it.
So let's have a start or let's start by having a look
at the layout extensions in the Katlin documentation.
So bring that up on screen, we'll open a browser.
Now firstly note these are experimental.
We've already used the past implementation
and we've talked about the experimental features.
LayoutContainer Support is another experimental feature
that's worth looking at here.
Now if for some reason LayoutContainer doesn't make it
into production we can just go back to using findViewByID.
We're learning here, we're not producing a production code
so I'm happy to show you these useful Katlin features.
But take that warning so you can see
on screen in that first paragraph though.
Seriously though, if you're working on an app that's going
to be released then don't turn on the experimental mode.
Alright, so moving down the screen LayoutContainer Support
so as it says virtually any class can be turned
into an Android Extensions container.
Example code shows us how to do that.
So let's do it back in our TaskViewHolder.
So we'll go back to Android Studio
and we'll do the same thing effectively.
So I'm gonna start by changing the definition on line 12.
For the argument we're gonna put an override there.
So override val container, our container colon View
then we need to add a comma to the
after the right parenthesis on line 13
and then we need to add LayoutContainer just interface.
And now that I've done that I can actually delete line 16
which I'll do now.
So effectively that leaves an empty class body.
And Android Studio suggests that we actually delete that.
You see its got the suggestion there for us.
But I won't do that now because we'll be adding
another function to this class a little bit later.
But that's it, that's all we need at the moment.
We're going to be adding on click lists
for the buttons later but we don't have to bother
with all those findViewByID calls which is pretty cool.
So let's just see, we've now got a reference
to the tli and it's code name TaskView
just like we have in our activities.
But the question here is will it work?
Well we'll see that in a moment.
First though we need to finish the code for the adapter
and we'll work on that in the next video.