我真的被困住了。
在我下面的代碼中,它向用戶顯示了他/她的購物車的內容并從用戶那里獲取資訊。一旦用戶滿意并輸入了所需的資訊,用戶就可以點擊繼續,這將打開一個新的活動。在這個新活動中,我想訪問用戶在上一個活動中輸入的資訊。
但是當我使用 Intent 時,它對兩個變數都說 NULL。在我Cart.kt的代碼/變數行中,我需要傳輸/傳遞給CheckoutScreen.kt
這是我的代碼:
Cart.kt(第一個活動)
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(第二個活動)
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()
}
}
uj5u.com熱心網友回復:
你正在把Int價值放在額外的東西上;您需要使用getIntExtra()來檢索它們。
請注意,它getIntExtra()需要兩個引數:額外的名稱和一個默認值,如果額外的沒有具有Int您指定名稱的 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/312837.html
上一篇:將流或序列轉換為摘要流或序列
