Android 如何在“回收器”视图中获取某些编辑文本的值?

Android 如何在“回收器”视图中获取某些编辑文本的值?,android,kotlin,android-recyclerview,Android,Kotlin,Android Recyclerview,我在recycler视图中有一些项目,每个项目都有一个编辑文本,我如何从每个编辑文本中获取值并将其传递给前一个片段,我尝试了一些解决方案,但没有一个真正适合我。 这是我的适配器: interface OnUpdateData{ fun dataUpdate (data: List<AcceptanceDifferent>?) } class EditAcceptDifferentAdapter(private val itemAcceptance: List<Dis

我在recycler视图中有一些项目,每个项目都有一个编辑文本,我如何从每个编辑文本中获取值并将其传递给前一个片段,我尝试了一些解决方案,但没有一个真正适合我。 这是我的适配器:


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
    }


}

更新数据接口{
乐趣数据更新(数据:列表?)
}
类EditAcceptDifferentAdapter(私有val项接受:列表,
私有val操作:onUpdate数据):
RecyclerView.Adapter(){
私有变量selectedItem=mutableSetOf()
private val selectedId=mutableSetOf()
私有lateinit变量listData:ArrayList
覆盖视图保持器(
父对象:视图组,
视图类型:Int
):主视窗支架{
返回主视图保持架(
LayoutInflater.from(parent.context)
.充气(R.layout.list_edit_different,parent,false)
)
}
重写getItemCount():Int{
返回项接受大小
}
@SuppressLint(“SetTextI18n”)
覆盖BindViewHolder(支架:MainViewHolder,位置:Int){
val项目=项目验收[位置]
如果(!items.expiredDate.isNullOrEmpty()){
val format2=“yyyy-MM-dd”
val localeID=Locale(“in”,“ID”)
val simpleDateFormat2=SimpleDateFormat(format2,localeID)
val date=simpleDataFormat2.parse(items.expiredDate)
holder.expiredDate.text=“过期日期:${SimpleDataFormat2.format(日期)}”
}
holder.batchCode.text=“批次代码:${items.inventory?.batchCode}”
holder.medName.text=项目.药物?.name
holder.batchCode.text=项目.库存?.batchCode
listData=ArrayList()
listData.add(AcceptanceDifferent(id=items.distPlanItemId,actualReceivedPackageQuantity=0))
操作.数据更新(listData)
}
内部类MainViewHolder(val视图:视图):RecyclerView.ViewHolder(视图){
val medName=view.textMedName1
val batchCode=view.tvBatchCode
val expiredDate=view.textmedExpiredDate 1
val数量=view.editTextQty
}
}
我制作了一个接口来传递数据,但在我的代码中,它不起作用


谢谢

您必须在EditText中定义一个侦听器才能调用数据更新

假设每次用户在任何EditText中键入任何内容时,您都希望在片段中接收更新的文本。在这种情况下,您需要在editText中添加一个
TextWatcher
,以调用您的
操作

override fun onBindViewHolder(holder: MainViewHolder,position: Int) {
    //...
    // Assuming you want every update in the `qty` value
    holder.qty.afterTextChanged { input ->
        action.onQtyUpdated(input)
    }
}
其中,
posterextchanged
是定义如下的扩展函数:

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())
        }
    })
}

您必须观察
EditText
中更改的文本,并在其中执行操作,然后通知侦听器检查数据。检查下面的实现。希望这对你有帮助

// 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)
    }
}

是否希望在列表中填充每个值时获取值?什么是
AcceptanceDifferent
外观?
数据类AcceptanceDifferent(private val id:Int?,private val actualReceivedPackageQuantity:Int?
@Md.Asaduzzaman,id来自数据集,EditText的实际接收数据包数量是您的
EditText
仅返回数字吗?是的,我如何将其传递到数组中?