简体   繁体   English

在Kotlin上的RecyclerView中保存EditText内容

[英]Saving EditText content in RecyclerView on Kotlin

Similarly to Saving EditText content in RecyclerView , I have an implementation of a pair of EditTexts to fill inside every row of a RecyclerView. 在RecyclerView中保存EditText内容类似,我实现了一对EditTexts的实现,以填充RecyclerView的每一行。 But my position variable inside my TextWatcher is always returning 0, even after the updatePosition function been called. 但是,即使在调用updatePosition函数之后,TextWatcher中的位置变量始终返回0。

the Lod.d at afterTextChanged() on both Watchers always shows that position is 0, even after filling the editText at the 3rd position. 即使在第3个位置填充editText之后,两个Watchers上的afterTextChanged()上的Lod.d始终显示该位置为0。

I can see that updatePosition goes from 0 to areasimportadas.size during the onBindViewHolder function, but it doesn't happen for onTextChanged. 我可以看到在onBindViewHolder函数执行期间updatePosition从0变为areaimportadas.size,但对于onTextChanged则不会发生。

class importItemsAdapter(val areasImportadas: MutableList, val inclinaLida: MutableList, val desvLido: MutableList) : RecyclerView.Adapter() { class importItemsAdapter(val areaImportadas:MutableList,val inclinaLida:MutableList,val desvLido:MutableList):RecyclerView.Adapter(){

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
   val layoutInflater = LayoutInflater.from(parent.context)
   val cellForRow = layoutInflater.inflate(R.layout.import_items,parent,false)
    return CustomViewHolder(cellForRow)
}

override fun getItemCount(): Int {
   return areasImportadas.size
}

override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
    holder.itemView.importNum.text = (position+1).toString()
    holder.itemView.importArea.text = areasImportadas[position]
    Watcher1().updatePosition(holder.adapterPosition)
    Watcher2().updatePosition(holder.adapterPosition)
    holder.itemView.importInclina.addTextChangedListener(Watcher1())
    holder.itemView.importDesv.addTextChangedListener(Watcher2())
}

class CustomViewHolder(view: View): RecyclerView.ViewHolder(view)

private inner class Watcher1 : TextWatcher {
    var position = 0

    fun updatePosition(positionExt: Int) {
        this.position = positionExt
        Log.d("Registro", this.position.toString())
    }

    override fun afterTextChanged(arg0: Editable) {
        inclinaLida[position]= arg0.toString()
        Log.d("Registro", "Inclinação $position: ${inclinaLida[position]}")
    }

    override fun beforeTextChanged(arg0: CharSequence, arg1: Int, arg2: Int, arg3: Int) {}

    override fun onTextChanged(s: CharSequence, a: Int, b: Int, c: Int) {

    }
}

private inner class Watcher2 : TextWatcher {
    var position = 0

    fun updatePosition(positionExt: Int) {
        position = positionExt
        Log.d("Registro", position.toString())
    }
    override fun afterTextChanged(arg0: Editable) {
        Log.d("Registro", "Desvio ${position}: ${desvLido[position]}")
    }

    override fun beforeTextChanged(arg0: CharSequence, arg1: Int, arg2: Int, arg3: Int) {
    }

    override fun onTextChanged(s: CharSequence, a: Int, b: Int, c: Int) {
        desvLido[position]=s.toString()
    }

}

} }

I needed to be able to store each EditText content under its proper position on both lists (desvLido and inclinaLida) 我需要能够将每个EditText内容存储在两个列表(desvLido和inclinaLida)的适当位置下

What was missing was calling an instance: 缺少的是调用一个实例:

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

    holder.itemView.importNum.text = (position+1).toString()
    holder.itemView.importArea.text = areasImportadas[position]
    val watcher1 = Watcher1()
    watcher1.updatePosition(holder.adapterPosition)
    holder.itemView.importInclina.addTextChangedListener(watcher1)
}

Now position returns the correct value. 现在position返回正确的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM