> For the complete documentation index, see [llms.txt](https://android-course.cornellappdev.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://android-course.cornellappdev.com/archive/archived-native-android-textbook-pages/4.-listview-and-recyclerview/4.5-implementation-with-input-controls.md).

# 2.4 Implementation with Input Controls

## Callback Interface

Often times, we’ll want to use the items in the `RecyclerView` as an entry point to another screen and the simplest way to do so is through an interface inside the adapter class.

{% hint style="info" %}
For this example, we'll be building on top of the code shown in section 4.3.
{% endhint %}

1\. We can define our own `onClick()` method in our interface and it can take in as many as parameters as we need.&#x20;

In this example, our click listener will just take in the position of the item on the list. Just remember that if you plan to use the `onClick()` method to start a new fragment or activity that you may want to pass in enough information, like a list or a key, to be able to make the correct computation.

{% code title="CustomAdapter.kt" %}

```kotlin
class CustomAdapter(
    private val dataSet: Array<Book>,
    private val mAdapterOnClickHandler: AdapterOnClickHandler
  )
    : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

  interface AdapterOnClickHandler {
    // you can define the parameters to be what you need
    fun onClick(position: Int)
  }
}
```

{% endcode %}

2\. Inside onBindViewHolder we can customize the onClickListeners of our various views to refer to the callback functions we just created! Below, we override the onClickListener of a button defined in our cell! We pass whatever is specified by the onClick method from our button.

{% code title="CustomAdapter.kt" %}

```kotlin
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
    viewHolder.button.setOnClickListener {
        mAdapterOnClickHandler.onClick(position)
    }
}
```

{% endcode %}

3\. In our activity or fragment, wherever the adapter is actually created, we’ll be implementing the `onClick()` method to our custom listener. In this example, the adapter will start a new activity when the item is clicked on.

Don't forget to extend the interface as we did at the header of the MainActivity! Android Studio will automatically tell you to implement the given functions once you do!

{% code title="MainActivity.kt" %}

```kotlin
class MainActivity : AppCompatActivity(),
   CustomAdapter.AdapterOnClickHandler {
   
   override fun onCreate(Bundle savedInstanceState) {
    ...
    recyclerView.adapter = CustomAdapter(myDataset, this)
    ...
  }
  
  override fun onClick(position: Int) {
    // add code for action to happen on click of the button in your RecyclerView row
    var i = Intent(this, SomeOtherActivity::class.java)
    i.putExtra("position", position)
    startActivity(i)
  }
}
```

{% endcode %}

## Higher-order functions

If your interface has a small amount of functions (i.e. one or two), the above can be simplified using higher-order functions!

Kotlin functions are [first-class](https://en.wikipedia.org/wiki/First-class_function), which means they can be stored in variables and data structures, and can be passed as arguments to and returned from other [higher-order functions](https://kotlinlang.org/docs/lambdas.html#higher-order-functions). You can perform any operations on functions that are possible for other non-function values.

Instead of defining some interface, we can pass the function in directly when creating a new adapter:

```kotlin
class CustomAdapter(
    private val dataSet: Array<Book>,
    private val onItemClick: (Int) -> Unit
  )
    : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
    
    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
    viewHolder.button.setOnClickListener {
        onItemClick(position)
    }
}
```

When initializing our adapter in our activity, we can pass in a function with the corresponding type or define it directly:

```kotlin
// An example that defines the function directly
val adapter = CustomAdapter(myDataset) { position -> 
   // The click action you want to perform.
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://android-course.cornellappdev.com/archive/archived-native-android-textbook-pages/4.-listview-and-recyclerview/4.5-implementation-with-input-controls.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
