Skip to main content

How to populate a RecyclerView with data in Kotlin Android

How to populate a RecyclerView with data in Kotlin Android.

Here's a step-by-step tutorial on how to populate a RecyclerView with data in Kotlin for Android:

Step 1: Set up your project

  • Create a new Android project in Android Studio.
  • Make sure you have the necessary dependencies in your app-level build.gradle file:
    dependencies {
    implementation 'androidx.recyclerview:recyclerview:1.2.0'
    }

Step 2: Create your data model

  • Create a data class or a POJO (Plain Old Java Object) to represent the data items you want to display in the RecyclerView. For example, let's create a "Person" data class with a name and age property:
    data class Person(val name: String, val age: Int)

Step 3: Design the layout for your RecyclerView item

  • Create a layout file for the RecyclerView item. For example, create a new XML file called "item_person.xml" and design it according to your needs. Make sure to include views to display the data from your data model.

Step 4: Create the RecyclerView adapter

  • Create a new Kotlin class for your RecyclerView adapter. This class will be responsible for inflating the item layout and binding data to the views. Let's call it "PersonAdapter".

  • Extend the RecyclerView.Adapter class and implement the necessary methods.

  • Here's an example implementation of the PersonAdapter:

    class PersonAdapter(private val persons: List<Person>) : RecyclerView.Adapter<PersonAdapter.PersonViewHolder>() {

    class PersonViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val nameTextView: TextView = itemView.findViewById(R.id.nameTextView)
    val ageTextView: TextView = itemView.findViewById(R.id.ageTextView)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PersonViewHolder {
    val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_person, parent, false)
    return PersonViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: PersonViewHolder, position: Int) {
    val person = persons[position]
    holder.nameTextView.text = person.name
    holder.ageTextView.text = person.age.toString()
    }

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

Step 5: Set up the RecyclerView in your activity or fragment

  • In your activity or fragment layout XML file, add a RecyclerView:

    <androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
  • In your activity or fragment class, initialize the RecyclerView and set its adapter:

    val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
    val adapter = PersonAdapter(persons) // assuming you have a list of Person objects called 'persons'
    recyclerView.adapter = adapter
    recyclerView.layoutManager = LinearLayoutManager(this)

Step 6: Provide data to the RecyclerView adapter

  • Create a list of your data objects (e.g., a list of Person objects) and pass it to the RecyclerView adapter when initializing it.

That's it! Your RecyclerView should now be populated with data. Make sure to update the data in your list (if needed) and call adapter.notifyDataSetChanged() to reflect any changes in the RecyclerView.

Note: This tutorial assumes you have basic knowledge of Android development and Kotlin programming.