Unresolved reference: textView

huangapple go评论94阅读模式
英文:

Unresolved reference: textView

问题

我正在尝试创建一个 RecyclerView,但是当我尝试引用我的 textView 时,出现了一个错误,提示“未解析的引用:textViewTitle”。我尝试了许多方法,但都没有成功,非常感谢您的智慧帮助。

class RecyclerAdapter(private val userList: ArrayList<Sitios> = ArrayList()) :
    RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.card_view, parent, false)

        return ViewHolder(v)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        // 未解析的引用:textViewTitle
        holder.itemView.textViewTitle.text = userList[position].description
    }

    override fun getItemCount(): Int = userList.size
}
英文:

I'm trying to create a recyclerView, but when I try to reference my textView I get an error that says Unresolved Reference: textViewTitle, I have tried many ways and I could not, I appreciate your wise help.

class RecyclerAdapter(private val userList: ArrayList&lt;Sitios&gt; = ArrayList()) :
    RecyclerView.Adapter&lt;RecyclerAdapter.ViewHolder&gt;() {

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.card_view, parent, false)

        return ViewHolder(v)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        //Unresolved reference: textViewTitle
        holder.itemView.textViewTitle.text = userList[position].description
    }

    override fun getItemCount(): Int = userList.size

答案1

得分: 4

holder.itemView是在执行onCreateViewHolder时传递给你的ViewHolder类的View。它只是一个普通的View,在该类中没有名为textViewTitle的字段,这就是你试图用holder.itemView.textViewTitle引用它时出现“引用未解析”的原因。

你可能想要做的是itemView的层次结构中查找具有该ID的TextView,所以你需要这样做:

holder.itemView.findViewById<TextView>(R.id.textViewTitle)

或者根据实际ID修改。但通常情况下,人们会将字段放在ViewHolder类中,通过一次查找进行设置,然后可以像你试图做的那样访问它们:

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    lateinit var textViewTitle: TextView
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val v = LayoutInflater.from(parent.context).inflate(R.layout.card_view, parent, false)
    // 或者你可以在ViewHolder的构造函数中将TextView设置为val,然后只需在创建后传递它,而不是在创建后设置它
    val holder = ViewHolder(v)
    holder.textViewTitle = v.findViewById<TextView>(R.id.textViewTitle)
    return ViewHolder(v)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    // 现在引用是ViewHolder的字段
    holder.textViewTitle.text = userList[position].description
}

这样做更好,因为onBindViewHolder每次新项滚动到视图中时都会运行,所以不要在可以只运行一次的情况下频繁执行findViewById


如果你像上面那样使用字段的方式,而不是将视图引用作为构造函数参数(当你有几个参数时可能会变得混乱),我会这样写:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val v = LayoutInflater.from(parent.context).inflate(R.layout.card_view, parent, false)
    return ViewHolder(v).apply {
        textViewTitle = v.findViewById(R.id.textViewTitle)
        // 和其他需要设置的视图
    }
}

看起来很整洁尤其是对于多个视图
英文:

holder.itemView is the View you pass into your ViewHolder class when onCreateViewHolder runs. It's just a normal View, and there's no field called textViewTitle in that class, which is what you're trying to reference with holder.itemView.textViewTitle - that's why it's saying the reference is unresolved

What you probably want to do is find a TextView with that ID within itemView's hierarchy, so you need to do

holder.itemView.findViewById&lt;TextView&gt;(R.id.textViewTitle)

or whatever the ID is. But typically, people will put fields in the ViewHolder class instead, set them by doing the lookup once, and then you can just access them like you're trying to do:

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    lateinit var textViewTitle : TextView
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val v = LayoutInflater.from(parent.context).inflate(R.layout.card_view, parent, false)
    // or you could make the TextView a val in the ViewHolder&#39;s constructor and just pass it in instead of setting it after creation
    val holder = ViewHolder(v)
    holder.textViewTitle = v.findViewById&lt;TextView&gt;(R.id.textViewTitle)
    return ViewHolder(v)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    // now the reference is a field on the ViewHolder
    holder.textViewTitle.text = userList[position].description
}

this is better because onBindViewHolder runs every time a new item scrolls into view, so you don't want to be doing lots of findViewByIds when you can just do it once


If you're doing it the field way, like up there, instead of making the view references constructor parameters (which can get messy when you have a few) I'd write it more like this:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val v = LayoutInflater.from(parent.context).inflate(R.layout.card_view, parent, false)
    return ViewHolder(v).apply {
        textViewTitle = v.findViewById(R.id.textViewTitle)
        // and any other views you need to set up
    }
}

it looks pretty neat, especially with several views!

huangapple
  • 本文由 发表于 2020年10月18日 23:19:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64414970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定