free geoip
50

Fix RecyclerView Not Showing in Kotlin

If your RecyclerView is not displaying data in your Kotlin Android app, don’t worry — this guide will walk you…

If your RecyclerView is not displaying data in your Kotlin Android app, don’t worry — this guide will walk you through the most common causes and effective solutions. Whether you’re a beginner or have experience with Android development, RecyclerView issues can be frustrating. Below, we’ll cover how to fix them by checking your Adapter, LayoutManager, and dataset connection. You’ll also get a working example with clean, modular Kotlin code.

RecyclerView not showing Kotlin

Common Causes of RecyclerView Not Displaying:

  • Adapter not set or initialized
  • LayoutManager missing or not set properly
  • Data list is empty or not updated
  • UI updates not triggered via notifyDataSetChanged()
  • View binding or XML layout misconfiguration

Working Kotlin Example (Complete Code)

1. Model Class – User.kt

data class User(val name: String, val email: String)

2. Adapter Class – UserAdapter.kt

class UserAdapter(private val userList: List<User>) : RecyclerView.Adapter<UserAdapter.UserViewHolder>() {

    inner class UserViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val nameText: TextView = itemView.findViewById(R.id.tvName)
        val emailText: TextView = itemView.findViewById(R.id.tvEmail)
    }

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

    override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
        val user = userList[position]
        holder.nameText.text = user.name
        holder.emailText.text = user.email
    }

    override fun getItemCount(): Int = userList.size
}

3. Layout File – item_user.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:textSize="16sp"/>

    <TextView
        android:id="@+id/tvEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Email"
        android:textSize="14sp"/>
</LinearLayout>

4. Activity File – MainActivity.kt

class MainActivity : AppCompatActivity() {

    private lateinit var recyclerView: RecyclerView
    private lateinit var adapter: UserAdapter
    private val userList = mutableListOf<User>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        recyclerView = findViewById(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(this)
        
        populateData()
        adapter = UserAdapter(userList)
        recyclerView.adapter = adapter
    }

    private fun populateData() {
        userList.add(User("Alice", "alice@example.com"))
        userList.add(User("Bob", "bob@example.com"))
        userList.add(User("Charlie", "charlie@example.com"))
    }
}

5. Activity Layout – activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"/>

🔗 Pro Tip

If you’re using LiveData or ViewModel, always observe changes and update the adapter accordingly. Learn more about LiveData updates in Android’s official documentation.

rysasahrial

Leave a Reply

Your email address will not be published. Required fields are marked *