1
00:00:05,060 --> 00:00:09,440
G.day everyone. Welcome back.
Our onClickListeners  are working,

2
00:00:09,440 --> 00:00:15,480
but they're not doing anything useful. We want them to notify the fragment or activity that the buttons were tapped.

3
00:00:15,480 --> 00:00:19,800
The way we'll do that is to define an interface in the adapter.

4
00:00:19,800 --> 00:00:23,680
MainActivityFragment will implement the functions in the interface,

5
00:00:23,680 --> 00:00:27,460
and we can be sure that it contains the functions that we're going to call.

6
00:00:27,460 --> 00:00:36,340
Let's declare an interface, and add our methods.

7
00:00:36,340 --> 00:00:43,620
We'll add one for onClickEdit, one for
clicking the delete button, and one for a long click.

8
00:00:43,620 --> 00:00:49,640
Now that we've defined the interface, we can pass in a reference to something that implements that interface,

9
00:00:49,640 --> 00:00:52,060
so that the adapter knows what to call.

10
00:00:52,060 --> 00:00:59,140
I'll add that to the primary constructor.

11
00:00:59,140 --> 00:01:05,060
Okay, we've got a listener property and
we can pass a reference to it, to our ViewHolder.

12
00:01:05,060 --> 00:01:08,920
The ViewHolder will then call the appropriate functions on the listener,

13
00:01:08,920 --> 00:01:12,280
and it will do that in its onClick functions.

14
00:01:12,280 --> 00:01:22,100
So let's add in the code we need.
First of all we we'll need to pass a listener into our bind function.

15
00:01:22,100 --> 00:01:40,960
Then we can replace the boilerplate that we put in earlier,
with the code that we actually want to use.

16
00:01:40,960 --> 00:01:47,580
We've now got an error in the onBindViewHolder function,

17
00:01:47,580 --> 00:01:58,980
and that's because bind is expecting an extra argument.
We need to pass in the listener,

18
00:01:58,980 --> 00:02:01,720
and that's the changes to the adapter finished.

19
00:02:01,720 --> 00:02:07,580
When one of the buttons is tapped, the onClick function will
call the corresponding function of our listener -

20
00:02:07,580 --> 00:02:10,419
the MainActivityFragment, in this case.

21
00:02:10,419 --> 00:02:14,380
The same happens if an item in the RecyclerView is long tapped.

22
00:02:14,380 --> 00:02:19,480
MainActivityFragment's onTaskLongClick function will be called.

23
00:02:19,480 --> 00:02:23,100
Before we finish this video, let's tidy up some logging.

24
00:02:23,100 --> 00:02:26,380
We know our adapter's working with the RecyclerView,

25
00:02:26,380 --> 00:02:42,120
so there's no need to log the onBindViewHolder
and getItemCount functions.

26
00:02:42,120 --> 00:02:49,400
In the next video, we'll get MainActivityFragment to implement the interface
and we'll add the required functions to it.

27
00:02:49,400 --> 00:02:52,600
See you in the next one

