0

The app is a Food Ordering App. I have added only few dishes from many in an array using intent.After I click proceed to cart button how can i see my dishes in another activity that are only added.

This is the code where i take the added dishes in array.

 val desc = dishList[position]
        holder.txtDishName.text = desc.desName
        holder.txtCost_for_one.text = desc.desCost_for_one
        holder.btnAdd.setOnClickListener {
            if (holder.btnAdd.text == "ADD") {
                val obj = Cart(
                    desc.id,
                    desc.desName,
                    desc.desCost_for_one,
                    desc.restaurant_id
                )
                addToCart.add(obj)
                holder.btnAdd.text = "Remove"
                val favColor = ContextCompat.getColor(this.context, R.color.colorPrimary)
                holder.btnAdd.setBackgroundColor(favColor)

            } else{
               for (i in 0 until addToCart.size ){
                   if (addToCart[i].cId == desc.id){
                       addToCart.removeAt(i)
                   }
               }
                holder.btnAdd.text = "Add"
                val favColor = ContextCompat.getColor(this.context, R.color.colorAccent)
                holder.btnAdd.setBackgroundColor(favColor)
            }
            val intent = Intent(context, DescriptionActivity::class.java)
            intent.putExtra("addToCart",addToCart)
            

        }
        
    }

This is the code where i click on the proceed to cart button.

btnProceed.setOnClickListener {
            val intent = Intent(this, CartActivity::class.java)
            //intent.getStringArrayExtra("addToCart")
            intent.getStringExtra("addToCart")
            startActivity(intent)

But after running the app the CartActivity comes Empty.

codebod
  • 686
  • 6
  • 17

1 Answers1

0

If you want use a string I think you should convert your list to json.

For example use Gson

var gson = Gson()
val myObject = myObject(...)
val jsonString = gson.toJson(myObject)
intent.putExtra("myObjectKey", jsonString)

And after that you can get string like

val jsonString = intent.getStringExtra("myObjectKey")
var myObject = gson.fromJson(jsonString, myObject::class.java)

For using Gson

implementation 'com.google.code.gson:gson:2.8.6'

MaxB
  • 635
  • 1
  • 9
  • 22