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 I am unable to access them in my MyCustomAdapter
class.
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 listOfCartItems = intent.getStringArrayListExtra("CuriseCart")
val sizeOfCart = listOfCartItems?.size
val listview = findViewById<ListView>(R.id.CartItems)
listview.adapter = MyCustomAdapter(this)
}
private class MyCustomAdapter(context: Context): BaseAdapter(){
private val mContext: Context
init {
mContext = context
}
override fun getCount(): Int {
return 5
}
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)
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 tried to create the intent in the adapter class, but that didn't work. Any advice or guidance will be appreciated.
CodePudding user response:
You are not passing your ArrayList
to your MyCustomAdapter
.
You can either make your MyCustomAdapter
class an inner
class or you can change the constructor of MyCustomAdapter
to accept your ArrayList
For making MyCustomeAdapter
an inner class
, just add inner
keyword to your class.
private inner class MyCustomAdapter(context: Context): BaseAdapter() {
//use listOfCardItem in the class
}
Or Add listOfCardItems
to your MyCustomeAdapter
constructor
private class MyCustomAdapter(context: Context, listOfCartItems: ArrayList<String>): BaseAdapter() {
//use listOfCardItem in the class
}
You 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 the init
block.
private class MyCustomAdapter(val context: Context, val listOfCartItems: ArrayList<String>)
CodePudding user response:
Solution 1:-
You can make your adapter an inner class by adding inner keyword to your class like this
private inner class MyCustomAdapter(context: Context): BaseAdapter() {//your code}
and make the list outside of your onCreate function.
Solution 2:-
just pass your list to the adapter like below
private class MyCustomAdapter(context: Context, listOfCartItems: ArrayList<YourModel>): BaseAdapter() {//your code}
In both ways you can access the your list.