简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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