G'day everyone. Welcome back.
Our adapter's now set up to call our fragment, when something interesting happens,
such as the user tapping a button.
In this video, we'll get the fragment to respond to the callbacks.
Looking at our CursorRecyclerViewAdapter,
we can see that it's expecting a listener that implements the onClickListener interface.
The first step then, is to get the MainActivityFragment to implement that interface.
We've got an error, as usual, but that's because we haven't implemented the interface functions yet.
We'll fix that in a moment. To begin with, there's another error when we create our CursorRecyclerViewAdapter
We added an extra parameter, so now we have to provide a listener.
Because MainActivityFragment implements the onTaskClickListener interface,
we can pass the current instance of it as the listener for the adapter.
That means our MainActivityFragment will be called back by the adapter, when a button's tapped.
As a reminder, that's why we use an interface.
The Kotlin compiler won't compile any class that says that it implements an interface,
but that then doesn't provide all the functions that the interface defines.
The interface makes sure that any object we pass as the listener to our adapter,
definitely does have all the functions that the adapter's going to call.
Okay, we'll get Android Studio to generate the interface functions, after onActivityCreated.
Control I is used to generate the functions that we need to implement, and we just select them all from the dialog.
I won't do this in the next bit of the video, but if you want to add logging to those functions, and run the app,
you'll see them being called when you tap the buttons.
So that's the easy bit done. Next we have to decide what these functions are going to do,
and that requires a bit of thought.
We'll start with the onEditClick.
What the user will want to happen, is that they can edit the task when they tap the edit button.
If you try to do that from here though, you'll end up in a real mess.
A Ffragment shouldn't attempt to add another Fragment to its Activity's layout.
Remember that it's the Activity that hosts the addEditFragment.
The only sensible thing to do here is to call back to MainActivity and let it take care of things.
That should be quite easy, because MainActivity is already doing everything it needs to do to edit the task.
We haven't edited an existing task yet, but the taskAditRequest function will edit if it's given a non-null task.
We're going to create a chain of callbacks.
Our adapter will call the onEditClick function in the MainActivityFragment,
and then MainActivityFragment will call another function in MainActivity.
To do that, MainActivityFragment will define an interface,
and MainActivity will implement it. And that sounds like a good challenge.
Define an interface in MainActivityFragment.
If you want to use the same name as me, I'll be calling mine OnTaskEdit, but you can call it anything you want.
The interface should define a single function, which takes a task as an argument.
Again, you can call it what you want, but I'll be calling mine onTaskEdit with a lowercase o.
Change MainAactivity to implement the interface, then add the required function.
When you've made the changes, run the app and test that tapping the edit button edits the correct task.
If you have problems casting the activity to the interface type, check out the earlier video in this section,
up and back navigation for fragments.
In that video, we covered casting the activity to an AppCompatActivity, and discussed how to do that properly.
In that example, we had a listener reference to the activity,
but the same principle applies here.
Just use activity rather than listener.
I'll stop this video here and go over the solution in the next video.
I'll see you there.