简体   繁体   中英

Scala type parameter error, not a member of type parameter

I am trying to implement a generic Adapter for Android in Scala. These are my classes:

class ScalaAdapter[A <: AnyRef](context: Context, resource: Int, objectsList: ArrayBuffer[A], adapterCb: AfAdapterItemCb) extends BaseAdapter with Filterable {...}

And the trait which every user of Adapter has to implement is:

trait AfAdapterItemCb {
  def itemCb[A](item: A, view: View) {}
}

My model class is:

case class ScheduleItem(name: String, priority: Int)

When I am implementing the Callback trait, I am getting an error related to type parameter:

  private class AdapterCbImpl extends AfAdapterItemCb {
    override def itemCb[ScheduleItem](schedule: ScheduleItem, view: View) = {      
      view.findViewById(R.id.tv_name).asInstanceOf[TextView].setText(schedule.name)
      view.findViewById(R.id.tv_priority).asInstanceOf[TextView].setText(schedule.priority.toString)
    }
  }

When I try to use a schedule object to fill the view, I am getting an error:

value name is not a member of type parameter ScheduleItem
value priority is not a member of type parameter ScheduleItem

I could not understand Manifest to fix it. Does someone understand what the problem is?

As Travis Brown noted, this is called shadowing , and can be extremely annoying .

override def itemCb[ScheduleItem](schedule: ScheduleItem, view: View) = {

ScheduleItem here is a type parameter name, just like T . It have nothing with case class ScheduleItem .

You can't override method with type parameter using concrete type.

I don't know how to use AfAdapterItemCb , but you could try something like this:

override def itemCb[T](item: T, view: View) = item match {
  case schedule: ScheduleItem => ... //your code
  case _ => ... // item is not a ScheduleItem
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM