Я понимаю, что мои layoutManager
и adapter
кажутся нулевыми, но я не совсем понимаю, почему это пусто.
вот код для моих GpsFragment.kt
class GpsFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
(context as AppCompatActivity).supportActionBar!!.title = "Gps"
val root = inflater.inflate(R.layout.fragment_gps, container, false)
showCars()
return root
}
private fun showCars(){
val cars = listOf(
Cars("Bezza","0.17263","3.267834"),
Cars("Bezza","0.17263","3.267834"),
Cars("Bezza","0.17263","3.267834"),
Cars("Bezza","0.17263","3.267834")
)
myCarlist?.layoutManager = LinearLayoutManager(activity)
myCarlist?.adapter = CarAdapter(cars)
}
Вот код для моей модели класса Cars.kt
data class Cars(val name: String = "-",
val latitude: String = "-",
val longtitude: String = "-")
А это код для моего адаптера CarAdapter.kt
class CarAdapter (private val cars : List<Cars>) : RecyclerView.Adapter<CarAdapter.CarViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CarViewHolder {
val inflater = LayoutInflater.from(parent.context).inflate(R.layout.list_layout, parent, false)
return CarViewHolder(inflater)
}
override fun getItemCount() = cars.size
override fun onBindViewHolder(holder: CarViewHolder, position: Int) {
val car = cars[position]
holder.view.carname.text = car.name
holder.view.latitude.text = car.latitude
holder.view.longtitude.text = car.longtitude
}
class CarViewHolder(val view:View) : RecyclerView.ViewHolder(view)
}