PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 4, 2022

[FIXED] How to use Lambda function in Android

 November 04, 2022     android, kotlin, lambda     No comments   

Issue

In my application I have RecyclerView adapter and I want when click on Items, send some data to parent.
I want send model and string.
I write below codes, but after use show me error and I can't use this!

Lambda function in adapter :

    private var onItemClickListener: ((UserEntity) -> Unit)? = null
private var actionType: String? = null

fun setOnItemClickListener(listener: (UserEntity) -> Unit, type: String) {
    onItemClickListener = listener
    actionType = type
}

Use this function in Activity :

noteAdapter.setOnItemClickListener { listener: UserEntity, type: String ->
        }

Error message Image :
enter image description here

How can I fix it and use this function ?


Solution

If you need access to both the objects i.e. NoteEntity & the String in the same listener, use like this :

private var onItemClickListener: ((UserEntity, String) -> Unit)? = null

fun setOnItemClickListener(listener: (UserEntity, String) -> Unit) {
    onItemClickListener = listener
}

After this, the lambda in the Activity should work fine.

noteAdapter.setOnItemClickListener { listener: UserEntity, type: String -> }


Answered By - DarShan
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing