1 00:00:04,580 --> 00:00:11,020 G'day everyone, welcome back again. Our app's coming along, and we've got a working ViewModel, 2 00:00:11,020 --> 00:00:14,960 and an adapter that provides data to the RecyclerView. 3 00:00:14,960 --> 00:00:19,340 Next, we'll make the edit and delete buttons do something. 4 00:00:19,340 --> 00:00:23,280 We'll start by getting them to display a message in the log. 5 00:00:23,280 --> 00:00:26,740 The principle is quite simple. They're just buttons, 6 00:00:26,740 --> 00:00:32,619 so we need to give them an onClick function, to execute whenever the user taps them. 7 00:00:32,619 --> 00:00:37,680 What we have to decide is where that onClick function should live, 8 00:00:37,680 --> 00:00:40,900 and what should happen when the buttons are clicked. 9 00:00:40,900 --> 00:00:46,000 So in this video, we'll set up the onClickListeners for our buttons. 10 00:00:46,000 --> 00:00:50,060 In the next video, we'll review callback functions 11 00:00:50,060 --> 00:00:56,080 and why we need to create an interface, before getting the buttons to actually do something useful. 12 00:00:56,080 --> 00:01:02,400 Let's have a look at the onBindViewHolder function 13 00:01:02,400 --> 00:01:07,680 in the CursorRecyclerViewAdapter class. 14 00:01:07,680 --> 00:01:13,840 If the TODO comments that were added haven't given this away, here's an obvious place to start. 15 00:01:13,840 --> 00:01:18,160 This is where we create the views for each row of the RecyclerAdapter, 16 00:01:18,160 --> 00:01:21,520 so we already have references to the buttons. 17 00:01:21,520 --> 00:01:27,980 We could set our onClickListeners for the buttons in this onBindViewHolder function, 18 00:01:27,980 --> 00:01:32,620 just like you set the listeners in an activity's onCreate, for example. 19 00:01:32,620 --> 00:01:39,860 Another approach, and the one we're going to use here, is to give our view holder a bind function. 20 00:01:39,860 --> 00:01:46,620 All this code is part of binding the data to the view, and bind is an obvious name for the function. 21 00:01:46,620 --> 00:01:51,840 We'll pass in the task, because ultimately we'll want to provide the task details 22 00:01:51,840 --> 00:01:55,380 to whatever is listening for the buttons to be tapped. 23 00:01:55,380 --> 00:02:00,680 Let's add the bind function. 24 00:02:00,680 --> 00:02:08,520 We'll set our values, and make sure the buttons are visible. 25 00:02:08,520 --> 00:02:21,900 Let's add the listeners. 26 00:02:21,900 --> 00:02:25,280 We're going to respond to long clicks a bit later, 27 00:02:25,280 --> 00:02:30,780 so I've added an onLongClickListener as well. 28 00:02:30,780 --> 00:02:36,220 The onLongClickListener has to return true if it has handled the tap, 29 00:02:36,220 --> 00:02:40,100 and that's the result of the lambda on line 37. 30 00:02:40,100 --> 00:02:47,020 The task needs to be available to our callback functions, because the activity, or fragment, that we callback 31 00:02:47,020 --> 00:02:51,020 will need to know which task we want to edit or delete. 32 00:02:51,020 --> 00:02:55,060 That's why we've passed it as the argument to the bind function. 33 00:02:55,060 --> 00:03:01,600 If we're gonna pass the task in here, it also makes sense to bind the task details to the widgets. 34 00:03:01,600 --> 00:03:04,340 That's what we're doing with the first bit of code. 35 00:03:04,340 --> 00:03:09,760 That means we don't need to set the data in onBindViewHolder. 36 00:03:09,760 --> 00:03:15,580 We need to replace these lines 37 00:03:15,580 --> 00:03:17,560 with the call to bind. 38 00:03:17,560 --> 00:03:29,060 That should work. Let's run it and check the logcat to see what we've got. 39 00:03:29,060 --> 00:03:35,380 Remember to clear any filter in the textbox. 40 00:03:35,380 --> 00:03:40,300 That should be easy to do, but there's currently a bug in Android Studio. 41 00:03:40,300 --> 00:03:45,220 You may find that the top toolbar for the logcat disappears. 42 00:03:45,220 --> 00:03:51,180 At the moment, a fix for that is to click in the event log tab in the bottom right. 43 00:03:51,180 --> 00:03:59,380 That displays the event log, and clicking it again removes the event log and puts back the logcat's toolbar. 44 00:03:59,380 --> 00:04:06,300 I'll demonstrate that if my toolbar disappears, but it looks like it hasn't. 45 00:04:06,300 --> 00:04:13,120 All right, each time I tap a button in the emulator we get an entry in the log cat. 46 00:04:13,120 --> 00:04:20,160 So it's working fine. A long tap on the task also works. 47 00:04:20,160 --> 00:04:26,480 We've got our buttons responding to taps, and we know which tasks the taps refer to. 48 00:04:26,480 --> 00:04:28,840 I'll stop this video here. 49 00:04:28,840 --> 00:04:36,420 In the next video, we'll get the buttons to do something more useful. See you in the next one.