I am creating an app where the user can add items to their cart. Once the user is done, they can click Proceed To Cart
which will then open up a new activity showing them the contents of their cart. The contents of the cart are stored in an array and I am passing the array to my second activity using an intent. But cannot access it my MyCustomAdapterClass.
Here is my code:
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ListView
import android.widget.TextView
import java.text.FieldPosition
class Cart : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cart)
val intent = intent
val listview = findViewById<ListView>(R.id.CartItems)
listview.adapter = MyCustomAdapter(this)
}
private 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)
println("Cart Items"+listOfCartItems)
println("Size of Cart"+sizeOfCart)
val cruiseTitle = rowFormat.findViewById<TextView>(R.id.cruiseTitle)
cruiseTitle.text = listOfCartItems?.get(sizeOfCart!!)
val durationLabel = rowFormat.findViewById<TextView>(R.id.durationLabel)
val numOfTravelers = rowFormat.findViewById<TextView>(R.id.numOfTravelers)
val costOfCruise = rowFormat.findViewById<TextView>(R.id.costOfCruise)
return rowFormat
}
}
}
I am trying to print whatever is in my array in the listview. I did some testing to see if the values were being passed by printing to console and it is correctly passing the values.
2
Answers
You are not passing your
ArrayList
to yourMyCustomAdapter
.You can either make your
MyCustomAdapter
class aninner
class or you can change the constructor ofMyCustomAdapter
to accept yourArrayList
For making
MyCustomeAdapter
aninner class
, just addinner
keyword to your class.Or Add
listOfCardItems
to yourMyCustomeAdapter
constructorYou don’t have to manually define a property in the class and initialize it with the received value in the primary constructor.
You can define your properties inside the primary constructor with
val
keyword for avoiding the manual initialization of them inside theinit
block.Solution 1:-
You can make your adapter an inner class by adding inner keyword to your class like this
and make the list outside of your onCreate function.
Solution 2:-
just pass your list to the adapter like below
In both ways you can access the your list.