Similarly to Saving EditText content in RecyclerView, I have an implementation of a pair of EditTexts to fill inside every row of a RecyclerView. But my position variable inside my TextWatcher is always returning 0, even after the updatePosition function been called.
the Lod.d at afterTextChanged() on both Watchers always shows that position is 0, even after filling the editText at the 3rd position.
I can see that updatePosition goes from 0 to areasimportadas.size during the onBindViewHolder function, but it doesn't happen for onTextChanged.
class importItemsAdapter(val areasImportadas: 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)