Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use "override fun onClick” inside onBindViewHolder in Kotlin?

I want the recycler view to listen to buttons click that exist in other fragments. Where I added the person to the database in the first fragment and the added person should be appeared in the recycler view of the second fragment after I click add.
The same code can work in java but can’t in Kotlin. The problem with the override of onClick ("Modifier ‘override’ is not applicable to ‘local function’").
Is there a solution to this issue?

The Code is:

override fun onBindViewHolder(holder: ViewHolder, position:Int) {
    holder.mRow = mData[position]
    holder.Personname.text = mData[position].name
    holder.Personage.text = mData[position].age.toString()

    holder.mView.setOnClickListener (
        View.OnClickListener {
            override fun onClick{
                if (null != mListener) {
                    mListener.onFragmentListenr(holder.mRow)
                }
            }
        }
    )
  }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Do like this…

holder.mView.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                mListener?.onFragmentListenr(holder.mRow)
            }
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading