I’m pretty new to programming and I’m struggling to figure out where I’ve gone wrong here.
Here is the piece of code in question
fun bind(bike: BikeShopModel) {
itemView.bikeTitle.text = bike.title
itemView.description.text = bike.description
}
its the bike.title part that is causing the issue.
Here is the full activity if it helps
package org.wit.bikeshop.activities
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_bikeshop.view.*
import kotlinx.android.synthetic.main.activity_bikeshop.view.bikeTitle
import kotlinx.android.synthetic.main.activity_bikeshop_list.*
import kotlinx.android.synthetic.main.card_bikeshop.view.*
import org.wit.bikeshop.R
import org.wit.bikeshop.main.MainApp
import org.wit.bikeshop.models.BikeShopModel
class BikeShopListActivity : AppCompatActivity() {
lateinit var app: MainApp
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_bikeshop_list)
app = application as MainApp
val layoutManager = LinearLayoutManager(this)
recyclerView.layoutManager = layoutManager
recyclerView.adapter = BikeShopAdapter(app.bikes)
}
}
class BikeShopAdapter constructor(private var bikes: List) :
RecyclerView.Adapter<BikeShopAdapter.MainHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainHolder {
return MainHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.card_bikeshop,
parent,
false
)
)
}
override fun onBindViewHolder(holder: MainHolder, position: Int) {
val bike = bikes[holder.adapterPosition]
holder.bind(bike)
}
override fun getItemCount(): Int = bikes.size
class MainHolder constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(bike: BikeShopModel) {
itemView.bikeTitle.text = bike.title
itemView.description.text = bike.description
}
}
Short answer:
You can set String to TextView using setText method. In your case it would look like this.
fun bind(bike: BikeShopModel) {
itemView.bikeTitle.setText(bike.title)
itemView.description.setText(bike.description)
}
1 Like
Thank you, I have it resolved now
1 Like