Android recycler view not calling any function (Kotlin)

I have a simple new application with a recycler view that should display “songs” but at the moment it displays non.
Non of the functions on the adapter are being called, not even getItemCount.

What ive tried:

  • ive set the background color of the recycler and I can see it appears on screen
  • ive set the layout manager to linear, i tried once on xaml and once on code but non solved the issue
  • ive set the adapter correctly and debugged that the construtor of the adapter is getting called and it is
  • ive debugged to check weteher the list of items are empty or getItemCount returns 0 and it is not
  • tried to call notifyDataSetChanged() after setting the adapter, no luck

I am not new to android, ive been developing on android xamarin, xamarin maui and now moved to android studio.
I cant seem to find anything wrong with my code. Can anyone help?
Thank you.

SongAdapter:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>/**
* Song adapter
*
* @property songs a given list of songs
* @constructor Create empty Song adapter
*/
class SongAdapter(private val songs: MutableList<Song> = mutableListOf()) : RecyclerView.Adapter<SongAdapter.SongVH>() {
/**
* Song view holder
*
* @property songDisplayView the song display view being held
* @constructor Create empty Song view holder
*/
class SongVH(val songDisplayView: SongDisplayView) : ViewHolder(songDisplayView){
}
override fun getItemCount(): Int {
return songs.count()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SongVH {
return SongVH(SongDisplayView(parent.context,null,songs[viewType]))
}
override fun onBindViewHolder(holder: SongVH, position: Int) {
holder.songDisplayView.setSong(songs[position])
}
/**
* Update songs to the given new songs
*
* @param newSongs given new songs
*/
fun updateSongs(newSongs: Iterable<Song>){
songs.clear()
songs.addAll(newSongs)
notifyItemRangeChanged(0,songs.size)
}
/**
* Adds a new song to the song list
*
* @param newSong a given new song
*/
fun addSong(newSong : Song){
songs.add(newSong)
notifyItemInserted(songs.size-1)
}
/**
* Removes a song from the song list
*
* @param index a given index to remove from
*/
fun removeSong(index: Int){
songs.removeAt(index)
notifyItemRemoved(index)
}
/**
* Updates the song at the given index to a new song
*
* @param song the new song
* @param index a given index
*/
fun updateSong(song: Song, index: Int){
songs[index] = song
notifyItemChanged(index)
}
}
</code>
<code>/** * Song adapter * * @property songs a given list of songs * @constructor Create empty Song adapter */ class SongAdapter(private val songs: MutableList<Song> = mutableListOf()) : RecyclerView.Adapter<SongAdapter.SongVH>() { /** * Song view holder * * @property songDisplayView the song display view being held * @constructor Create empty Song view holder */ class SongVH(val songDisplayView: SongDisplayView) : ViewHolder(songDisplayView){ } override fun getItemCount(): Int { return songs.count() } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SongVH { return SongVH(SongDisplayView(parent.context,null,songs[viewType])) } override fun onBindViewHolder(holder: SongVH, position: Int) { holder.songDisplayView.setSong(songs[position]) } /** * Update songs to the given new songs * * @param newSongs given new songs */ fun updateSongs(newSongs: Iterable<Song>){ songs.clear() songs.addAll(newSongs) notifyItemRangeChanged(0,songs.size) } /** * Adds a new song to the song list * * @param newSong a given new song */ fun addSong(newSong : Song){ songs.add(newSong) notifyItemInserted(songs.size-1) } /** * Removes a song from the song list * * @param index a given index to remove from */ fun removeSong(index: Int){ songs.removeAt(index) notifyItemRemoved(index) } /** * Updates the song at the given index to a new song * * @param song the new song * @param index a given index */ fun updateSong(song: Song, index: Int){ songs[index] = song notifyItemChanged(index) } } </code>
/**
 * Song adapter
 *
 * @property songs a given list of songs
 * @constructor Create empty Song adapter
 */
class SongAdapter(private val songs: MutableList<Song> = mutableListOf()) : RecyclerView.Adapter<SongAdapter.SongVH>() {

    /**
     * Song view holder
     *
     * @property songDisplayView the song display view being held
     * @constructor Create empty Song view holder
     */
    class SongVH(val songDisplayView: SongDisplayView) : ViewHolder(songDisplayView){
    }

    override fun getItemCount(): Int {
        return songs.count()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SongVH {
        return SongVH(SongDisplayView(parent.context,null,songs[viewType]))
    }

    override fun onBindViewHolder(holder: SongVH, position: Int) {
        holder.songDisplayView.setSong(songs[position])
    }


    /**
     * Update songs to the given new songs
     *
     * @param newSongs given new songs
     */
    fun updateSongs(newSongs: Iterable<Song>){
        songs.clear()
        songs.addAll(newSongs)
        notifyItemRangeChanged(0,songs.size)
    }

    /**
     * Adds a new song to the song list
     *
     * @param newSong a given new song
     */
    fun addSong(newSong : Song){
        songs.add(newSong)
        notifyItemInserted(songs.size-1)
    }

    /**
     * Removes a song from the song list
     *
     * @param index a given index to remove from
     */
    fun removeSong(index: Int){
        songs.removeAt(index)
        notifyItemRemoved(index)
    }

    /**
     * Updates the song at the given index to a new song
     *
     * @param song the new song
     * @param index a given index
     */
    fun updateSong(song: Song, index: Int){
        songs[index] = song
        notifyItemChanged(index)
    }
}

SongDisplayView:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class SongDisplayView(context: Context,attributeSet: AttributeSet?,song: Song = Song()) : LinearLayout(context,attributeSet) {
private val titleView: TextView = TextView(context)
init {
titleView.text = song.name
titleView.textSize = 18f
addView(titleView)
}
fun setSong(song: Song ) {
titleView.text = song.name
}
}
</code>
<code>class SongDisplayView(context: Context,attributeSet: AttributeSet?,song: Song = Song()) : LinearLayout(context,attributeSet) { private val titleView: TextView = TextView(context) init { titleView.text = song.name titleView.textSize = 18f addView(titleView) } fun setSong(song: Song ) { titleView.text = song.name } } </code>
class SongDisplayView(context: Context,attributeSet: AttributeSet?,song: Song = Song()) : LinearLayout(context,attributeSet) {

    private val titleView: TextView = TextView(context)
    init {
        titleView.text = song.name
        titleView.textSize = 18f
        addView(titleView)
    }

    fun setSong(song: Song ) {
        titleView.text = song.name
    }
}

MainActivity:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class MainActivity : AppCompatActivity() {
private lateinit var binding : ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
val songAdapter = SongAdapter(mutableListOf(Song("Song 1"),Song("Song 2"), Song("Song 3")))
binding.songsRecycler.adapter = songAdapter
songAdapter.notifyDataSetChanged()
}
}
</code>
<code>class MainActivity : AppCompatActivity() { private lateinit var binding : ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContentView(R.layout.activity_main) binding = ActivityMainBinding.inflate(layoutInflater) ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) insets } val songAdapter = SongAdapter(mutableListOf(Song("Song 1"),Song("Song 2"), Song("Song 3"))) binding.songsRecycler.adapter = songAdapter songAdapter.notifyDataSetChanged() } } </code>
class MainActivity : AppCompatActivity() {

    private lateinit var binding : ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)

        binding = ActivityMainBinding.inflate(layoutInflater)

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val songAdapter = SongAdapter(mutableListOf(Song("Song 1"),Song("Song 2"), Song("Song 3")))
        binding.songsRecycler.adapter = songAdapter
        songAdapter.notifyDataSetChanged()
    }
}

activity_main layout:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:text="hello world"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:background="#fadada"
android:id="@+id/songsRecycler"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</code>
<code><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:text="hello world" android:layout_width="match_parent" android:layout_height="wrap_content"/> <androidx.recyclerview.widget.RecyclerView android:background="#fadada" android:id="@+id/songsRecycler" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </code>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity" >
<TextView
    android:text="hello world"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
    <androidx.recyclerview.widget.RecyclerView
        android:background="#fadada"
        android:id="@+id/songsRecycler"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

You’re able to see the background color of the recycler because you initially set the layout using setContentView(R.layout.activity_main).

But you inflated the same activity_main layout again using viewBinding. Then you setup the recycler (binding.songsRecycler) from this newly inflated layout.

The newly inflated layout remains invisible until you set it as the content view using setContentView(binding.root). That’s why recycler items aren’t displayed.

Solution

Just set binding.root in setContentView.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class MainActivity : AppCompatActivity() {
private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(binding.root)
// other codes ...
}
}
</code>
<code>class MainActivity : AppCompatActivity() { private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContentView(binding.root) // other codes ... } } </code>
class MainActivity : AppCompatActivity() {

    private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(binding.root)
        // other codes ...
    }
}

1

Maybe this is the issue: setContentView(R.layout.activity_main). You should do setContentView(binding.root) after binding field is assigned.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật