Home > Software design >  Intent Passing Null Variables to Second Activity
Intent Passing Null Variables to Second Activity

Time:10-14

I am really stuck.

In my code below, it shows the user the contents of his/her cart and gets information from the user. Once the user is satisfied and has entered the neededd information, then the user can click on continue, which will open up a new activity. In this new activity, I want to access the information the user entered in the previous activity.

But when I use Intent, it says NULL for both variables. In my Cart.kt I have put a comment on the line of code/variable that I need to transfer/pass to CheckoutScreen.kt

Here is my code:

Cart.kt (first activity)

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*


class Cart : AppCompatActivity() {
    var numKids = 0
    var numAdults = 0
    var totalPax = 0
    var getSubtotal = 0.0
    var displayTax = 0.0
    var finTotal = 0.0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_cart)

        val listview = findViewById<ListView>(R.id.CartItems)
        listview.adapter = MyCustomAdapter(this)


    }

    inner class MyCustomAdapter(context: Context): BaseAdapter() {
        private val mContext: Context

        val listOfCartItems = intent.getStringArrayListExtra("CuriseCart")
        val sizeOfCart = listOfCartItems?.size

        init {
            mContext = context
        }

        override fun getCount(): Int {
            return sizeOfCart!!
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun getItem(position: Int): Any {
            return "Testing"
        }

        override fun getView(position: Int, convertView: View?, viewGroup: ViewGroup?): View {
            val layoutInflater = LayoutInflater.from(mContext)
            val rowFormat = layoutInflater.inflate(R.layout.listview_custom, viewGroup, false)

            val cruiseTitle = rowFormat.findViewById<TextView>(R.id.cruiseTitle)
            cruiseTitle.text = listOfCartItems?.get(position)

            numAdults = intent.getStringExtra("numAdults")?.toInt()!!
            numKids = intent.getStringExtra("numKids")?.toInt()!!
            totalPax = numAdults   numKids // I need to pass this to second act

            val durationLabel = rowFormat.findViewById<TextView>(R.id.durationLabel)
            val costOfCruise = rowFormat.findViewById<TextView>(R.id.costOfCruise)
            val totalPaxTV = rowFormat.findViewById<TextView>(R.id.totalPaxCount)
            val subtotal = findViewById<TextView>(R.id.subtotal)
            val tax = findViewById<TextView>(R.id.tax)


            var total = findViewById<TextView>(R.id.total)

            if (listOfCartItems?.get(position).equals("Bahamas Cruise")) {
                val getDurationOfCruise = getString(R.string.bahamasDuration)
                val getCostOfCruise = getString(R.string.bahamasCPrice)
                val intCost = getString(R.string.bahamasCPriceInt)

                durationLabel.text = getDurationOfCruise
                totalPaxTV.text = (totalPax.toString()   " Passengers")
                costOfCruise.text = getCostOfCruise

                getSubtotal = (getSubtotal   intCost.toDouble())
                getSubtotal = String.format("%.2f", getSubtotal).toDouble()

            } else if (listOfCartItems?.get(position).equals("Caribbean Cruise")) {
                val getDurationOfCruise = getString(R.string.caribbeanDuration)
                val getCostOfCruise = getString(R.string.caribbeanCPrince)
                val intCost = getString(R.string.caribbeanCPrinceInt)

                durationLabel.text = getDurationOfCruise
                totalPaxTV.text = (totalPax.toString()   " Passengers")
                costOfCruise.text = getCostOfCruise

                getSubtotal = (getSubtotal   intCost.toDouble())
                getSubtotal = String.format("%.2f", getSubtotal).toDouble()

            } else if (listOfCartItems?.get(position).equals("Cuba Cruise")) {
                val getDurationOfCruise = getString(R.string.cubaDuration)
                val getCostOfCruise = getString(R.string.cubaCPrice)
                val intCost = getString(R.string.cubaCPriceInt)

                durationLabel.text = getDurationOfCruise
                totalPaxTV.text = (totalPax.toString()   " Passengers")
                costOfCruise.text = getCostOfCruise

                getSubtotal = (getSubtotal   intCost.toDouble())
                getSubtotal = String.format("%.2f", getSubtotal).toDouble()

            } else if (listOfCartItems?.get(position).equals("Sampler Cruise")) {
                val getDurationOfCruise = getString(R.string.samplersDuration)
                val getCostOfCruise = getString(R.string.samplersPrice)
                val intCost = getString(R.string.samplersPriceInt)

                durationLabel.text = getDurationOfCruise
                totalPaxTV.text = (totalPax.toString()   " Passengers")
                costOfCruise.text = getCostOfCruise

                getSubtotal = (getSubtotal   intCost.toDouble())
                getSubtotal = String.format("%.2f", getSubtotal).toDouble()

            } else if (listOfCartItems?.get(position).equals("Star Cruise")) {
                val getDurationOfCruise = getString(R.string.starDuration)
                val getCostOfCruise = getString(R.string.starCPrice)
                val intCost = getString(R.string.starCPriceInt)

                durationLabel.text = getDurationOfCruise
                totalPaxTV.text = (totalPax.toString()   " Passengers")
                costOfCruise.text = getCostOfCruise

                getSubtotal = (getSubtotal   intCost.toDouble())
                getSubtotal = String.format("%.2f", getSubtotal).toDouble()
            }

            displayTax = getSubtotal * 0.13
            finTotal = displayTax   getSubtotal // I need to pass this to second act

            subtotal.text = "$"   getSubtotal.toString()
            tax.text = "$"   String.format("%.2f", displayTax)
            total.text = "$"   String.format("%.2f", finTotal)
            return rowFormat

        }
    }

    fun continueToCheckoutInformation(view: View) {
        val intent = Intent(this@Cart, CheckoutScreen::class.java)
        intent.putExtra("toalPax", totalPax)
        intent.putExtra("finTotal", finTotal)
        startActivity(intent)
    }
}

CheckoutScreen.kt (second activity)

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast

class CheckoutScreen : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_checkout_screen)
        
        val pax = intent.getStringExtra("toalPax")
        val cst = intent.getStringExtra("finTotal")

        Toast.makeText(applicationContext, "Testing = $pax and $cst", Toast.LENGTH_SHORT).show()

    }
}

CodePudding user response:

You are putting Int values into the extras; you need to use getIntExtra() to retrieve them.

Note that getIntExtra() takes two parameters: the name of the extra and a default value to return if the extra does not have an Int with your specified name.

  • Related