Home > database >  How to return nothing from lambda in Kotlin on Android? (Type mismatch)
How to return nothing from lambda in Kotlin on Android? (Type mismatch)

Time:03-27

So I got this function in Kotlin:

 private fun initComponents(){
        lv = findViewById(R.id.listView)

        lv?.setOnItemLongClickListener { _, _, i, _ ->
            removeItem(i) <- Error Here
        }
    }

fun removeItem(index : Int){
        items.removeAt(index) 
    }

where items is an ArrayList.

And it gives me an error in the lambda -

Type mismatch. Required: Boolean Found: Unit

I'm new to Kotlin. Thanks for any help

CodePudding user response:

You need to return a Boolean value to indicate if you consumed the event or not:

        lv?.setOnItemLongClickListener { _, _, i, _ ->
            removeItem(i)
            true
        }
  • Related