Home > database >  How to pass arraylist from recyclerview to Other Activity - kotlin?
How to pass arraylist from recyclerview to Other Activity - kotlin?

Time:04-22

I'm trying to pass multiple value of arraylist, which are in recyclerview to Another Activity. But every time I get a null value or either memory location instead of values. Here is my adapter code


class ContactAdapter(private var contact:List<ContactModel>, private var context: Context, private val showMenu: (Boolean) -> Unit): RecyclerView.Adapter<ContactAdapter.ContactViewHolder>() {
    private var isEnable =false
    private val itemSelected = mutableListOf<Int>()
    val myMutableContactList = ArrayList<ContactModel>()
    class ContactViewHolder(private val view: View):RecyclerView.ViewHolder(view) {
        val linlay:LinearLayout = view.findViewById(R.id.linlay)
        val name: TextView = view.findViewById(R.id.name)
        val number: TextView = view.findViewById(R.id.number)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContactViewHolder {
        val layout_ =LayoutInflater.from(parent.context).inflate(R.layout.contact_list,parent,false)
        return ContactViewHolder(layout_)
    }

    override fun onBindViewHolder(holder: ContactViewHolder, position: Int) {
        val item = contact[position]
        holder.name.text = item.name.toString()
        holder.number.text = item.number.toString()
        holder.linlay.setOnLongClickListener{
            selectItem(holder,item,position)
            true
        }
        holder.linlay.setOnClickListener{

            if(itemSelected.contains(position)){
              itemSelected.remove(position)

                holder.linlay.setBackgroundColor(Color.DKGRAY)
                item.isSelect = false
                if(itemSelected.isEmpty()){
                    isEnable=false
                }
            }
            else if(isEnable){
                selectItem(holder,item,position)
            }
        }}
    private fun selectItem(holder: ContactAdapter.ContactViewHolder, item: ContactModel, position: Int) {
        isEnable = true
        itemSelected.add(position)
        item.isSelect = true
        showMenu(true)
        holder.linlay.setBackgroundColor(Color.YELLOW)
        if(item.isSelect){
            myMutableContactList.add(item)}
        else{
            myMutableContactList.remove(item)}}

    override fun getItemCount(): Int {
        return contact.size
    }
    fun sendSelectedItem(){
        if(itemSelected.isNotEmpty()){
            val intent = Intent(context,HomeActivity::class.java)
            intent.putParcelableArrayListExtra("key",myMutableContactList)
            context.startActivity(intent)
            isEnable = false
            itemSelected.clear()
        } }}

Value is passed after selecting multiple items and the button for next activity is in the menu bar. I'm using getter setter model class instead of the data class here is my model class

class ContactModel() :Parcelable {
    var name: String? = null
    var number: String? = null
    var isSelect: Boolean = false
    constructor(parcel: Parcel) : this() {
        name = parcel.readString()
        number = parcel.readString()
        isSelect = parcel.readByte() != 0.toByte()
    }
    fun setNames(name: String) {
        this.name = name
    }
    fun getNumbers(): String {
        return number.toString()
    }
    fun setNumbers(number: String) {
        this.number = number
    }
    fun getNames(): String {
        return name.toString()
    }

code of other activity

  private var list_: ArrayList<ContactModel>? = null
  list_ = intent.getParcelableArrayListExtra("key") 
         val contactModel = ContactModel()
        val name = contactModel.getName()
        val number =  contactModel.getNumbers()
        list_!!.add(contactModel)
        Log.d("name>>", name   "  "   number)
        Toast.makeText(this, name.toString(), Toast.LENGTH_SHORT).show()

CodePudding user response:

I have an idea but not sure is best practice or not. You can create a companion object in the activity where you want to send the data and in the companion object create a variable of type lateinit var. You can initialize it even when activity is not active from the adapter.

CodePudding user response:

If you read the documentation of this function, it says:

The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".

which you did not do, since you're simply using "key".

You should not have a bunch of "get" and "set" functions in your class that redundantly manage your properties. "getter" and "setter" functions are not used in Kotlin because properties fill that role.

Your Parcelable class is lacking a CREATOR function and a writeToParcel function, so it cannot work. You should consider using the Parcelize plugin so you don't have to add those yourself.

  • Related