首页 > 解决方案 > 如何在视图绑定中的活动中获取回收者视图当前项目文本值

问题描述

我有一个回收器视图,我需要获取由文本视图和其他视图组成的当前 recyclerView 项目的值。在视图绑定之前,它非常简单,如下所示:

recyclerview.findViewHolderForAdapterPosition(i)!!.itemView.mDescription.text.toString()

现在在 recyclerView 适配器中启用视图绑定后,我无法从活动中访问 editTextDescription。任何帮助表示赞赏。

这是我的适配器,其中包含 textView、editText、复选框等。我想要的是在单击 Activity 中的按钮时,它应该从特定位置的项目的 recyclerView 适配器(ListAdapter)获取数据并将其提交给 REST api。

class SpiranAdapter(private val list: List<Spiran>)
    : RecyclerView.Adapter<SpiranViewHolder>() {
   override fun onBindViewHolder(holder: SpiranViewHolder, position: Int) {
        val spiran: Spiran = list[position]
        holder.bind(spiran)
    }
}
class SpiranViewHolder(inflater: LayoutInflater, parent: ViewGroup) :
    RecyclerView.ViewHolder(inflater.inflate(R.layout.list_item, parent, false)) {
    private var mTitleView: TextView? = null

    init {
        mTitleView = itemView.findViewById(R.id.list_title) // textView
        mYearView = itemView.findViewById(R.id.list_description) //textView
        mDescription = itemView.findViewById(R.id.description) // as EditText
        isServiceOpted = itemView.findViewById(R.id.isServiceOpted) // checkbox
    }
    fun bind(spiran: Spiran) {
        mTitleView?.text = spiran.title
        mYearView?.text = spiran.year.toString()
        mDescription?.setText(spiran.description);
        isServiceOpted as Checkable).isChecked = spiran.status
    }

}

除了复选框,没有任何点击监听器。有editText,我需要从那个editText到活动的价值

标签: androidandroid-recyclerviewandroid-viewholder

解决方案


我认为您应该在这里使用用户界面。


推荐阅读