LunaVulpo
LunaVulpo

Reputation: 3221

Out-projected type in generic interface prohibits the use of metod with generic parameter

I start using Kotlin and I defined interface like this:

   interface AAdapter<VH : RecyclerView.ViewHolder> {

        fun onCreateAViewHolder(parent: ViewGroup): VH

        fun onBindAViewHolder(v: VH, position: Int)
    }

and when I try to use it on code:

class Klasa (
        private val adapter: AAdapter<*>
) {

   fun doSth(){
      //... 
      val vh = this.adapter.onCreateAViewHolder(parent)
      //on below line I get error
      adapter.onBindAViewHolder(v, position)
     //...
  }

}

I get error that Out-projected type 'AAdapter<*>' prohibits the use of 'public abstract fun onBindAViewHolder(v: T, position: Int): Unit

I tied to add "in" or "out" to definition but I'm confused.

How to allow it.

Upvotes: 0

Views: 946

Answers (1)

Bob
Bob

Reputation: 13865

Replace AAdapter<*> with AAdapter<RecyclerView.ViewHolder>.

class Klasa (
        private val adapter: AAdapter<RecyclerView.ViewHolder>
) {
 // ...

Because you have declared that AAdapter has a type parameter VH whose type is any subclass of RecyclerView.ViewHolder. And in the class Klasa you have an instance of AAdapter which has type parameter of any type.

Upvotes: 1

Related Questions