0
votes

I have two Fragment that communicate with each other from viewmodel .

the detail fragment is going to pass data to cart fragment every time that user click on additem .

cart fragment has a recyclerview to show this passed data .

with passing the data to cart fragment there is no problem .

when user add a item to cart each item must take a position and so on . but in here my item replace with each other and not take any position . So that's what I want if user click on one item and after that another item , each item take a position in recyclerview not replace with each othere ?

Here is Code.

my detail fragment :

 lateinit var model: Model
 val viewmodel: ViewmodelTest by activityViewModels()

 // with this bundle I pass data from my activity to my fragment
  val bundle = arguments
        val name = bundle?.getParcelable<Model>("list")
        model = name


// when user click on this btn must send this list to cart fragment 

view.plus_btn.setOnClickListener {
number = 0
number++
 val list = ArrayList<ModelCart>()

                list.add(ModelCart(model.id , number , model.title , model.price , model.image))

                viewmodel.selectedItem(list)
}

CartFragment :

val viewmodel: ViewmodelTest by activityViewModels()
    lateinit var recyclerView: RecyclerView



     recyclerView = viewLayout.recyclerView_cart
        recyclerView.layoutManager =
            LinearLayoutManager(viewLayout.context, LinearLayoutManager.VERTICAL, false)
        
        // this livedata observe new data

        viewmodel.selected.observe(viewLifecycleOwner, Observer { model ->
            recyclerView.adapter = CartAdapter(model)

        })

I'm not sure to put here adapter code or not . just in case if u need .

CartAdapter :

class CartAdapter(private val model: List<ModelCart>) :
    RecyclerView.Adapter<CartAdapter.ViewItemHolder>() {
    
    lateinit var context: Context

    class ViewItemHolder(itemView: View) :
        RecyclerView.ViewHolder(itemView) {
        
        val title: TextView = itemView.title_cart
        val price: TextView = itemView.price_cart
        val image: ImageView = itemView.imageView_cart
        val amount: TextView = itemView.amount
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewItemHolder {

        context = parent.context
        val layout =
            LayoutInflater.from(parent.context).inflate(R.layout.row_item_cart, parent, false)
        return ViewItemHolder(layout)

    }

    override fun getItemCount() = model.size

    override fun onBindViewHolder(holder: ViewItemHolder, position: Int) {

        val mposition = model[position]

        holder.title.text = mposition.title
        holder.price.text = mposition.price
        holder.image.setImageResource(mposition.image)
        holder.amount.text = mposition.amount.toString()

    }
}

I will appreciate to anyone who can help with this .

1

1 Answers

0
votes

You need to declare your list outside of onClickListener() otherwise it will create a new instance of list every time :

val list = ArrayList<ModelCart>()
view.plus_btn.setOnClickListener {
    number = 0
    number++             
    list.add(ModelCart(model.id , number , model.title , model.price , model.image))
    viewmodel.selectedItem(list)
}