简体   繁体   English

如何在 Kotlin 的适配器 class 中应用视图绑定?

[英]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.

相关问题 我可以使用Kotlin Android Extension推迟适配器中的视图绑定吗 - Can I defer the view binding in an adapter using Kotlin Android Extension 如何使用 Kotlin 的视图绑定在 Android 中缩短此代码? - How can I shorten this code in Android using view binding by Kotlin? 如何获取不在ListView可见部分中的适配器类内部的视图? -科特林 - How get view inside adapter class that is not in visible part of ListView? - Kotlin 如何在 Kotlin 中使用 `getString()` 从 Recycler Adapter 类访问 `strings.xml` 中的字符串 - How can I access strings in `strings.xml` from a Recycler Adapter class using `getString()` in Kotlin 如何将 ViewBinding 与 RecyclerView 适配器与 Kotlin 中已经存在的内部 class 一起使用? - How can I use ViewBinding with RecyclerView Adapter with an already existing inner class in Kotlin? 如何在绑定适配器中将此 java 代码写入 kotlin - How to write this java code to kotlin in Binding Adapter 如何在 android kotlin 中创建合并适配器 class - How can create merge adapter class in android kotlin Kotlin:如何从Adapter类中调用DialogFragment? - Kotlin: How do I call a DialogFragment from within an Adapter class? 如何在Kotlin的RecyclerView的自定义适配器中调用setColorFilter? - How can I call setColorFilter in RecyclerView's custom Adapter in Kotlin? 当我将函数setVisible(View.gone)应用于适配器时,如何从与适配器对应的列表中删除空格? - How to remove the space from a list corresponding to an adapter, when I apply the function setVisible (View.gone) to the adapter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM