在Kotlin主类中显示onBindViewHolder值

时间:2018-10-25 06:50:26

标签: android kotlin

我正在尝试在主类中获取onBindViewHolder变量值,但不确定如何调用此变量。任何帮助表示赞赏。

override fun onBindViewHolder(p0: TestDetailMenuViewHolder, p1: Int) {
    val Testmenudetail = TestMenudetails.get(p1)
    p0?.customView?.itemname.text = Testmenudetail.price.toString()

    var menuname: String = Testmenudetail.menu
    var itemprice: String = Testmenudetail.price.toString()
    //Let's say itemprice=50
}
class TestMenuDetail() : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        var Test1 = itemprice //???
    }
}

1 个答案:

答案 0 :(得分:0)

尝试这样做:

class Adapter : RecyclerView.Adapter<Holder> {
  var itemprice = 0 

  override fun onBindViewHolder(p0: TestDetailMenuViewHolder, p1: Int) {
    val Testmenudetail = TestMenudetails.get(p1)
    p0?.customView?.itemname.text = Testmenudetail.price.toString()

    var menuname: String = Testmenudetail.menu
    var itemprice: String = Testmenudetail.price.toString()
    itemprice=50
  }
}

然后:

class TestMenuDetail() : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    var itemprice = Adapter().itemprice
  }
}

但是我不明白为什么需要从适配器获取变量。您需要知道方法onBindViewHolder被频繁调用,并且值itemprice将会改变。