Issue
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.
Solution
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>)
Answered By - Praveen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.