G.day everyone. Welcome back. Our onClickListeners are working,
but they're not doing anything useful. We want them to notify the fragment or activity that the buttons were tapped.
The way we'll do that is to define an interface in the adapter.
MainActivityFragment will implement the functions in the interface,
and we can be sure that it contains the functions that we're going to call.
Let's declare an interface, and add our methods.
We'll add one for onClickEdit, one for clicking the delete button, and one for a long click.
Now that we've defined the interface, we can pass in a reference to something that implements that interface,
so that the adapter knows what to call.
I'll add that to the primary constructor.
Okay, we've got a listener property and we can pass a reference to it, to our ViewHolder.
The ViewHolder will then call the appropriate functions on the listener,
and it will do that in its onClick functions.
So let's add in the code we need. First of all we we'll need to pass a listener into our bind function.
Then we can replace the boilerplate that we put in earlier, with the code that we actually want to use.
We've now got an error in the onBindViewHolder function,
and that's because bind is expecting an extra argument. We need to pass in the listener,
and that's the changes to the adapter finished.
When one of the buttons is tapped, the onClick function will call the corresponding function of our listener -
the MainActivityFragment, in this case.
The same happens if an item in the RecyclerView is long tapped.
MainActivityFragment's onTaskLongClick function will be called.
Before we finish this video, let's tidy up some logging.
We know our adapter's working with the RecyclerView,
so there's no need to log the onBindViewHolder and getItemCount functions.
In the next video, we'll get MainActivityFragment to implement the interface and we'll add the required functions to it.
See you in the next one