Home > Enterprise >  How can I apply the view binding in adapter class in Kotlin?
How can I apply the view binding in adapter class in Kotlin?

Time:01-16

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. 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

and

Variable expected

for text

CodePudding user response:

When you use View Binding, each layout XML class generates a "binding" class named after it. So R.layout.row_center has a class called RowCenterBinding (underscores are removed, words are capitalised, and it ends with Binding.

You create an instance of a binding class by calling its static inflate method, like this:

// 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. 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). It also has a property for every View with an ID in the XML file. (These get renamed the same way that row_center.xml -> RowCenter does.)

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!

(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))


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.

(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)

  •  Tags:  
  • Related