I'm new in Kotlin and I making an app which is currency changer. And in the adapter, I want to pass some items into a new activity.
class AdapterC (val countryList: ArrayList<CountriesData>): RecyclerView.Adapter<AdapterC.ViewHolder>() {
override fun onCreateViewHolder(view: ViewGroup, position: Int): ViewHolder {
val v = LayoutInflater.from(view?.context).inflate(R.layout.country_list,view,false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return countryList.size
}
override fun onBindViewHolder(view: ViewHolder, position: Int) {
val country : CountriesData=countryList.toTypedArray()[position]
view?.textViewName.text=country.name
/*
Old code in which i can accese to items
view.itemView.setOnClickListener{
var name = country.name
var id = country.id
}
*/
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
val textViewName= itemView.findViewById(R.id.TextViewCountry) as TextView
//New code that i found on the internet
init {
itemView.setOnClickListener{
val intent = Intent(itemView.context, CurrencyActivity::class.java)
itemView.context.startActivity(intent)
}
}
}
}
As I know that is a bad practice to put setOnClickListener inside onBindViewHolder and I couldn't start a new activity in there so I looked on the Internet and there was the solution to start new Activity inside ViewHolder class. But now I don't know how to pass an item, that was clicked into the new activity.
Below is data class
data class CountriesData(val name :String,val id :String)