'How to return nothing from lambda in Kotlin on Android? (Type mismatch)
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
Solution 1:[1]
You need to return a Boolean value to indicate if you consumed the event or not:
lv?.setOnItemLongClickListener { _, _, i, _ ->
removeItem(i)
true
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CommonsWare |
