Home > Software design >  Visibility modifier conflict in interface declaration in Kotlin
Visibility modifier conflict in interface declaration in Kotlin

Time:03-23

I have a simple recycler view where I have Interface within the body and I have declared the interface in constructor parameter so I can access it within the recycler view's body. But it shows the error as

Conflicting declarations: public interface CardClickListener, private final val CardClickListener: MemoryBoardAdapter.CardClickListener

I have tried to change the visibility modifiers but still the error persists. ** My Code:**

Recycler View With Interface:

class MemoryBoardAdapter(   private val CardClickListener: CardClickListener ) :     RecyclerView.Adapter<MemoryBoardAdapter.ViewHolder>() { 

interface CardClickListener{     

    fun onCardClicked(position: Int) 

    } }

MainActivity object calling the recyclerView:

binding.rvBoard.adapter = MemoryBoardAdapter(object: MemoryBoardAdapter.CardClickListener{override fun onCardClicked(position: Int) {// TODO}

        })

I tried to change from private val to internal var but still the error persists. if I remove the visibility modifier in the class constructor the error goes away but I cant access the interface within the class body.

CodePudding user response:

The capital C for CardClickListener in object: MemoryBoardAdapter.CardClickListener is conflicting with the interface name CardClickListener inside your adapter. Change the name of the parameter or use cardClickListener

class MemoryBoardAdapter(private val cardClickListener: CardClickListener ) 

instead of

class MemoryBoardAdapter(private val CardClickListener: CardClickListener ) 
  • Related