[英]How can I apply the view binding in adapter class in Kotlin?
I am try to apply view binding in my project, I have like that adapter class, but I did not understand how I will apply.我正在尝试在我的项目中应用视图绑定,我喜欢那个适配器 class,但我不明白我将如何应用。 Any idea?
任何想法?
class Center : AppCompatActivity() {
private lateinit var binding: ActivityCenterBinding
var listView: ListView? = null
private var mTitle = arrayOf("Help", "Help2", "Help3")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityCenterBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
listView = binding.help_listView
val adapter = MyAdapter(this, mTitle)
listView!!.adapter = adapter
internal inner class MyAdapter(
context: Center,
private var rTitle: Array<String>,
) : ArrayAdapter<String?>(context, R.layout.row_center, R.id.textView_center, rTitle) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val layoutInflater =
applicationContext.getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater
val binding = ActivityCenterBinding.inflate(layoutInflater, parent, false)
val myTitle = binding.textView_center
myTitle.text = rTitle[position]
return binding.root
}
}
the problem is问题是
Unresolved reference: textView__center
未解决的参考:textView__center
and和
Variable expected
预期变量
for text对于文本
When you use View Binding, each layout XML class generates a "binding" class named after it.当您使用视图绑定时,每个布局 XML class 都会生成一个以它命名的“绑定” class。 So
R.layout.row_center
has a class called RowCenterBinding
(underscores are removed, words are capitalised, and it ends with Binding
.所以
R.layout.row_center
有一个名为 RowCenterBinding 的RowCenterBinding
(删除了下划线,单词大写,并以Binding
结尾。
You create an instance of a binding class by calling its static inflate
method, like this:您可以通过调用 static
inflate
方法来创建绑定 class 的实例,如下所示:
// in an Activity
val binding = RowCenterBinding.inflate(layoutInflater)
// in anything else - you pass the thing the View is being inflated into (parent)
// and whether it should actually be added to that parent (no)
val binding = RowCenterBinding.inflate(layoutInflater, parent, false)
Now you have a RowCenterBinding
object.现在你有一个
RowCenterBinding
object。 It has a root
property, which is the view hierarchy you inflated (which you need to return at the end of your getView
method in the question).它有一个
root
属性,它是您膨胀的视图层次结构(您需要在问题中的getView
方法的末尾返回它)。 It also has a property for every View
with an ID in the XML file.它还具有 XML 文件中 ID 的每个
View
的属性。 (These get renamed the same way that row_center.xml -> RowCenter
does.) (这些重命名方式与
row_center.xml -> RowCenter
相同。)
So you end up with a binding
object that holds your View```s (
binding.root ) and also references to the ones with IDs (
binding.textView1 ,
binding.saveButton`` etc.) That's pretty much it!所以你最终得到了一个
binding
object ,它包含你的View```s (
) and also references to the ones with IDs (
( binding.textView1 ,
binding.saveButton 等) 就是这样!
(If you already have a view hierarchy inflated from an XML file, like in an Activity
, you can create the binding object from that with RowCenterBinding.bind(view)
) (如果您已经拥有从 XML 文件扩展的视图层次结构,例如在
Activity
中,则可以使用RowCenterBinding.bind(view)
创建绑定 object )
You keep editing the question and inflating different things, so just do this:你一直在编辑问题并夸大不同的东西,所以这样做:
internal inner class MyAdapter(
context: Center,
private var rTitle: Array<String>,
) : ArrayAdapter<String?>(context, R.layout.row_center, R.id.textView_center, rTitle) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val layoutInflater =
applicationContext.getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater
// You're inflating row_center.xml, so RowCenterBinding is the class you want
// Don't specify a type here unless you know what you're doing - this is fine!
val binding = RowCenterBinding.inflate(layoutInflater, parent, false)
// this requires a view in the XML with an android:id of textViewCenter,
// text_view_center, or something like that. Let autocomplete help you here!
val myTitle = binding.textViewCenter
// this should work fine once myTitle is assigned to a TextView
myTitle.text = rTitle[position]
// this method requires an inflated View, so we return the root view
return binding.root
}
}
Since you have a variable called binding
in your outer activity too, you might want to call this one rowBinding
or something instead, so you don't confuse them.由于您的外部活动中也有一个名为
binding
的变量,因此您可能希望将其称为rowBinding
或其他名称,以免混淆它们。
(Also you shouldn't need to do any of this for a basic ArrayAdapter
, doesn't it Just Work if you delete the getView
method? By default it should create a view and set the text on a TextView using the constructor parameters you're passing) (此外,您不需要对基本的
ArrayAdapter
执行任何此操作,如果您删除getView
方法,它不就可以工作吗?默认情况下,它应该创建一个视图并使用您的构造函数参数在 TextView 上设置文本'重新通过)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.