Home > Software design >  Cant seem to get my adapter to add my list to recyclerView
Cant seem to get my adapter to add my list to recyclerView

Time:10-05

Id like to initially have a list show up on my recyclerView before implementing some kind of edit function to it. However, no matter what I do, the list I made wont show up. I've got a feeling that I'm doing something wrong with the bind function.

Here is my adapter

class WorkoutAdaptor (
    var workouts: List<Workout>
    ) : RecyclerView.Adapter<WorkoutAdaptor.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val workoutCardBinding = WorkoutCardBinding.inflate(
            LayoutInflater.from(parent.context), parent, false)

        return ViewHolder(workoutCardBinding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(workouts[position])
    }

    override fun getItemCount(): Int {
        return workouts.size
    }

    inner class ViewHolder(private val workoutCardBinding: WorkoutCardBinding) :
        RecyclerView.ViewHolder(workoutCardBinding.root) {
        fun bind(workout: Workout) {
            workoutCardBinding.apply {
                tvWorkoutCard
            }
        }
    }
}

and here is my main activity

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.btnNext.setOnClickListener {
            Intent(this, SecondActivity::class.java).also {
                startActivity(it)
            }
        }
//        var workout = intent.getSerializableExtra("EXTRA_WORKOUT")
//        binding.tvWorkoutCard.text = workout.toString()
        var workoutList = mutableListOf(
            Workout("a","d","d","d"),
            Workout("a","d","d","d"),
            Workout("a","d","d","d"),
            Workout("a","d","d","d"),
            Workout("a","d","d","d"),
        )

        val adaptor = WorkoutAdaptor(workoutList)
        binding.recyclerView.adapter = adaptor
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
    }
}

CodePudding user response:

Change your bind method to this.

fun bind(workout: Workout) {
 workoutCardBinding.textview.text = workout.objectName
}
  • Related