简体   繁体   中英

How to pass multiple EditText values from RecyclerView adapter to main Activity?

I want to pass multiple EditText values from RecyclerView to main Activity. I used BroadcastReceiver to handle this. And addTextChangedListener is used to get the changing text from the EditText. Here I shared my code. Please check and suggest me a solution.

Adapter code:

holder.textBox.addTextChangedListener(object : TextWatcher {
    override fun afterTextChanged(p0: Editable?) {
        var texts = ArrayList<String>()
        texts.add(p0.toString())
        val intent = Intent("custom-message")
        intent.putStringArrayListExtra("quantity", texts)
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent)

    }

    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
})

MainActivity code:

val broadCastReceiver = object : BroadcastReceiver() {
    override fun onReceive(contxt: Context?, intent: Intent?) {
        qty = intent!!.getStringArrayListExtra("quantity")
        Log.d("broadCastReceiver", qty.toString())
    }
}

LocalBroadcastManager.getInstance(this)
    .registerReceiver(broadCastReceiver, IntentFilter("custom-message"))

Just use a callback function.

  1. make a callback interface
  2. make sure that your main activity implements that callback interface.
  3. from recycleview adapter, create an object of callback function which is present in interface.
  4. call the callback function instead of brodcast reciver when text is changed.

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