I am trying to display recyclerview list data using adapter. So far there is no error and I think my code logic is correct, but it didn't show the list when I run the app.
Below is my MainActivity code
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val activityMainBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(activityMainBinding.root)
setSupportActionBar(activityMainBinding.mainToolbar)
binding = activityMainBinding.contentMain
val rv = binding.recyclerView
val clickListener = { view: View, bandName:String ->
//Deal with click feature later, show the list first
}
adapter = RVAdapter(clickListener)
rv.adapter = adapter
rv.layoutManager = LinearLayoutManager(this)
adapter.submitlist(bandResources.values.toMutableList())
Below is my RVAdapter code
class RVAdapter(private val clickListener: (view:View, bandName: String)->Unit)
: ListAdapter<BandInfo,RVAdapter.ViewHolder>(Diff())
{
inner class ViewHolder(val bandRowBinding : BandRowBinding)
: RecyclerView.ViewHolder(bandRowBinding.root) {
init {
bandRowBinding.root.setOnClickListener{
//Deal with click feature later, show the list first
}
}
}
private var bands = mutableListOf<BandInfo>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val bandRowBinding = BandRowBinding.inflate(LayoutInflater.from(parent.context),
parent, false)
return ViewHolder(bandRowBinding)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val binding = holder.bandRowBinding
bands[position].let{
binding.bandTitle.text = it.name
binding.bandCreated.text = it.time
}
}
fun submitlist(items: MutableList<BandInfo>){
bands = items
}
Where did I go wrong?
When using ListAdapter
, you don't need to store the Band list in adapter.
And you don't need to declare another submitlist
function because ListAdapter already had it, just simply call submitList()
function from ListAdapter
itself.