Vector
Vector

Reputation: 3235

android kotlin lambda to add two integers

We are trying to use a lambda expression in android with Kotlin to add two integers. We have looked at the docs. Made our syntax verbatim to the docs. Here is the output that is set in a EditText field.

Function2<java.lang.Integer, java.lang.Integer, java.lang.Integer>

Our code is implemented with a onClick event of a button. Code Below

val x:Int = 5
val y:Int = 17
// Set at top level
fun onINHRclass(view: View){

    val sumB = {x:Int,y:Int -> x + y}
    println("======================"+sumB)
    etANSpg2.setText(sumB.toString())
}

The question is how to use a lambda to add two integers and set the result in a EditText field?

We have looked at and tried variations of other posts and other tutorials the lambda works on the website Try Kotlin but it does not have a EditText field

Upvotes: 0

Views: 1147

Answers (1)

jaychang0917
jaychang0917

Reputation: 1888

The question is how to use a lambda to add two integers and set the result in a EditText field?

sumB is like a function, so you should call it like a function:

val x:Int = 5
val y:Int = 17
// Set at top level
fun onINHRclass(view: View){

    val sumB = {x:Int,y:Int -> x + y}
    println("======================"+sumB)
    etANSpg2.setText(sumB(x, y).toString())
}

Upvotes: 3

Related Questions