0

I have implemented this interface in my Adapter in Kotlin:

interface OnItemClickListener {
    fun onItemClick(view: View)
}

public lateinit var onItemClickListener: OnItemClickListener

public fun setItemClickListener(itemClickListener: OnItemClickListener) {
    this.onItemClickListener = itemClickListener
}

I want to use it in my Fragment with Lambda like this:

    adapter.setItemClickListener {
        // do stuff here
    }

But this error appears:

Error:(54, 39) Type mismatch: inferred type is () -> Unit but SSUpdatesAdapter.OnItemClickListener was expected

I know that I can just implement Adapter.AddOnItemTouchListener but I really want to know more about Kotlin and how to use Lambdas

0

1 Answer 1

2

The feature you are trying to use (single function interface to lambda) only works when the interface was written in Java code.

The reasoning behind this is this:

"since Kotlin has proper function types, automatic conversion of functions into implementations of Kotlin interfaces is unnecessary and therefore unsupported"

https://kotlinlang.org/docs/reference/java-interop.html#sam-conversions

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.