What kind of parameter that can pass variables inside a Unit in Kotlin?

PleaseDon'tBanMe

I am confuse about what kind of parameter should I pass from the main class to the recycler adapter class that have parameter type like this

class RecyclerAdapter(private val clickListener:(view: View, movieName: String)
    ->Unit):ListAdapter<MovieInfo,RecyclerAdapter.ViewHolder>(Diff())

In the main class, my plan is to declare the adapter like below

val adapter = RecyclerAdapter() //missing parameter inside the ()

I have never seen a class parameter like that before. Usually, it is a starightforward list data type such as RecyclerAdapter(private val dressList: List<DressList.DressName>). Maybe if I know some examples about that kind of parameter, it can give me some enlightenment about what parameter should I pass. Thanks.

cactustictacs

Just to break this down

val name: String

That's simple enough - the variable is called name, and its type is String. If that's a parameter, you need to pass a String there.

val clickListener: () -> Unit

The type here is () -> Unit, which is a function. You know how you call a function by putting () after the name, like toString()? That's what that represents in the type, a function that takes no parameters. The -> is showing the type it produces (or returns if you like). This is a function that takes no parameters, and produces no value (aka Unit).

val clickListener: (view: View, movieName: String) -> Unit

This is similar, except this type is a function that does take parameters - a View and a String, and returns nothing. When you call this function, you're expected to pass those values into it, and the function does something with them.


The setup you have here is basically an Adapter that's displaying MovieInfo objects, and in the constructor you pass in a listener function. So when the user clicks an item, that function gets called with the relevant values. It means a component outside of the adapter can decide what actually happens when something gets clicked, by providing a function that does the necessary stuff. The adapter doesn't need to know any of those details - it just needs to pass the listener function the data it requires.


I don't know what you're doing with this:

val clickListener = movieList.fold(Unit, { view, movieName -> Unit })

but if you hover over clickListener you'll see its inferred type is Unit. This isn't a function object, this is the result of calling the fold function. You've run fold over your movieList, basically returning Unit at each step, and the result is Unit. That's what you're trying to pass as your clickListener parameter.

A function would be in { } braces (or a function reference like ::handleClick), like this:

val clickListener = { view, movieName -> // do stuff that returns unit }

and if you really wanted to do a fold for some reason, you can do it in there! But it's usually a function that does something like display a movie, add it to some list or a database, that kind of thing. An action in response to a click

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can we pass java method to to kotlin function of type () -> Unit?

What kind of kwargs can you pass to a QObject during initialization?

What are the kind of variables that must be disposed? (.NET/Java)

Pass interface as parameter in Kotlin

What kind of sorting does Kotlin use by default?

How can the scope of jq 'as' variables be extended to pass down inside functions?

Can't pass php variables in parameter in a HTML onclick

Pass variables to a function inside Evaluate()

What does `Unit = Unit` do and mean in Kotlin?

Kotlin pass KType as generic parameter

How to pass a parameter inside of an optionalChain

Can't understand specific kind of kotlin functions

How can I pass a parameter inside a condition in Julia?

can local variables inside a sealed object pass information to global variables?

What kind of screwdriver can unscrew this?

Can't pass GET parameter while unit testing Zend Framework 2

How can I pass an instance of a basetype and then check what kind of subtype it is?

What can you pass into the parameter map for CommunityService.getMyCommunities?

What kind of patterns can I use in zsh parameter expansion?

Can I pass Variables inside IN Clause when using pivot operator

Can't pass global variables inside a function

What is this kotlin feature?: Kotlin extension properties as parameter type: `fun X.func(X.() -> Unit)`

What object should I pass to a function that requires Void! in a parameter? (Kotlin)

I can pass dictionary as action parameter but can't pass it inside complex object

What kind of datestyle can this be?

Pass variables inside render to outside?

Pass request model inside of a parameter

Can't initialize variables inside of when statement in Kotlin

What kind of material is used to ground the inside of the laptop?