Reputation: 1246
I have some items in recycler view and each item has an edit text, how I get the value from each edit text and pass it to the previous fragment, I've tried some solution but none of them really work for me. here is my adapter:
interface OnUpdateData{
fun dataUpdate (data: List<AcceptanceDifferent>?)
}
class EditAcceptDifferentAdapter(private val itemAcceptance: List<DistributionPlanItems>,
private val action: OnUpdateData) :
RecyclerView.Adapter<EditAcceptDifferentAdapter.MainViewHolder>() {
private var selectedItem = mutableSetOf<AcceptanceDifferent>()
private val selectedId = mutableSetOf<Int>()
private lateinit var listData: ArrayList<AcceptanceDifferent>
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): MainViewHolder {
return MainViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.list_edit_different, parent, false)
)
}
override fun getItemCount(): Int {
return itemAcceptance.size
}
@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: MainViewHolder,position: Int) {
val items = itemAcceptance[position]
if (!items.expiredDate.isNullOrEmpty()) {
val format2 = "yyyy-MM-dd"
val localeID = Locale("in", "ID")
val simpleDateFormat2 = SimpleDateFormat(format2, localeID)
val date = simpleDateFormat2.parse(items.expiredDate)
holder.expiredDate.text = "Expired Date: ${simpleDateFormat2.format(date)}"
}
holder.batchCode.text = "Batch Code: ${items.inventory?.batchCode}"
holder.medName.text = items.medicine?.name
holder.batchCode.text = items.inventory?.batchCode
listData = ArrayList()
listData.add(AcceptanceDifferent(id = items.distPlanItemId, actualReceivedPackageQuantity = 0))
action.dataUpdate(listData)
}
inner class MainViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
val medName = view.textMedName1
val batchCode = view.tvBatchCode
val expiredDate = view.textMedExpiredDate1
val qty = view.editTextQty
}
}
I've made an interface to pass the data but in my code, it doesn't work
thank you
Upvotes: 1
Views: 967
Reputation: 15423
You have to watch text changed in your EditText
and do your operation there and then notify the listener to check the data. Check below implementation. Hope this will help you.
// You have to initialize listData outside of onBindViewHolder
listData = ArrayList()
@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: MainViewHolder,position: Int) {
val items = itemAcceptance[position]
// Other code here
holder.batchCode.text = "Batch Code: ${items.inventory?.batchCode}"
holder.medName.text = items.medicine?.name
holder.batchCode.text = items.inventory?.batchCode
//listData = ArrayList()
holder.qty.doAfterTextChanged { text ->
val data = listData.find { it.id == items.distPlanItemId }
val value = text?.toString()?.toInt()
if(data == null) {
listData.add(AcceptanceDifferent(id = items.distPlanItemId, actualReceivedPackageQuantity = value))
} else {
data.actualReceivedPackageQuantity = value
}
action.dataUpdate(listData)
}
}
Upvotes: 1
Reputation: 778
You have to define a listener in the EditText to call the data update.
Let's say you want to receive the updated text in your fragment every time that the user types anything in any EditText. In that case you will need a TextWatcher
in your editText that will call your action
.
override fun onBindViewHolder(holder: MainViewHolder,position: Int) {
//...
// Assuming you want every update in the `qty` value
holder.qty.afterTextChanged { input ->
action.onQtyUpdated(input)
}
}
Where the afterTextChanged
is an extension function defined below:
fun EditText.afterTextChanged(afterTextChanged: (String) -> Unit) {
this.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
override fun afterTextChanged(editable: Editable?) {
afterTextChanged.invoke(editable.toString())
}
})
}
Upvotes: 0