我正在尝试在主类中获取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 //???
}
}
答案 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
将会改变。