G'day everyone, welcome back again. Our app's coming along, and we've got a working ViewModel,
and an adapter that provides data to the RecyclerView.
Next, we'll make the edit and delete buttons do something.
We'll start by getting them to display a message in the log.
The principle is quite simple. They're just buttons,
so we need to give them an onClick function, to execute whenever the user taps them.
What we have to decide is where that onClick function should live,
and what should happen when the buttons are clicked.
So in this video, we'll set up the onClickListeners for our buttons.
In the next video, we'll review callback functions
and why we need to create an interface, before getting the buttons to actually do something useful.
Let's have a look at the onBindViewHolder function
in the CursorRecyclerViewAdapter class.
If the TODO comments that were added haven't given this away, here's an obvious place to start.
This is where we create the views for each row of the RecyclerAdapter,
so we already have references to the buttons.
We could set our onClickListeners for the buttons in this onBindViewHolder function,
just like you set the listeners in an activity's onCreate, for example.
Another approach, and the one we're going to use here, is to give our view holder a bind function.
All this code is part of binding the data to the view, and bind is an obvious name for the function.
We'll pass in the task, because ultimately we'll want to provide the task details
to whatever is listening for the buttons to be tapped.
Let's add the bind function.
We'll set our values, and make sure the buttons are visible.
Let's add the listeners.
We're going to respond to long clicks a bit later,
so I've added an onLongClickListener as well.
The onLongClickListener has to return true if it has handled the tap,
and that's the result of the lambda on line 37.
The task needs to be available to our callback functions, because the activity, or fragment, that we callback
will need to know which task we want to edit or delete.
That's why we've passed it as the argument to the bind function.
If we're gonna pass the task in here, it also makes sense to bind the task details to the widgets.
That's what we're doing with the first bit of code.
That means we don't need to set the data in onBindViewHolder.
We need to replace these lines
with the call to bind.
That should work. Let's run it and check the logcat to see what we've got.
Remember to clear any filter in the textbox.
That should be easy to do, but there's currently a bug in Android Studio.
You may find that the top toolbar for the logcat disappears.
At the moment, a fix for that is to click in the event log tab in the bottom right.
That displays the event log, and clicking it again removes the event log and puts back the logcat's toolbar.
I'll demonstrate that if my toolbar disappears, but it looks like it hasn't.
All right, each time I tap a button in the emulator we get an entry in the log cat.
So it's working fine. A long tap on the task also works.
We've got our buttons responding to taps, and we know which tasks the taps refer to.
I'll stop this video here.
In the next video, we'll get the buttons to do something more useful. See you in the next one.