I am a junior programmer here trying to create an MVVM application in Android using Kotlin. I am trying to create a housing app that retrieves houses from an API then shows them in a recycler view.
The problem is that my recycler view is empty. I do have an adapter and maybe there is something going wrong. I believe they're not being paired together correctly
Anyway, I'm going to show my fragment class.
class HousesFragment : Fragment() {
private lateinit var factory: HousesViewModelFactory
private lateinit var viewModel: HousesViewModel
private lateinit var rv: RecyclerView
private lateinit var adapter: HousesAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.houses_fragment, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val api = HouseApi()
val repo = HouseRepo(api)
factory = HousesViewModelFactory(repo)
viewModel = ViewModelProviders.of(this, factory).get(HousesViewModel::class.java)
rv = view?.findViewById(R.id.rvHouses) ?: return
//use the adapter
viewModel.getHouses()
viewModel.houses.observe(viewLifecycleOwner, Observer { houses ->
rv.also {
it.layoutManager = LinearLayoutManager(requireContext())
it.adapter = HousesAdapter(houses, this)
it.setHasFixedSize(true)
rv.adapter = it.adapter
}
})
}
}
I think that this code in particular is causing the problem
rv.also {
it.layoutManager = LinearLayoutManager(requireContext())
it.adapter = HousesAdapter(houses, this)
it.setHasFixedSize(true)
rv.adapter = it.adapter
}
the reason why I say that is because this code is responsible for matching the recyclerview with the adapter.
I'll explain other things I tried. I tried to implement other people's code and see how their's works. I did so by going on Youtube and checking a bunch of tutorials. Nothing has worked so far, and it's not ideal to do this. I'm trying my best to get past this error. Looking at examples is the best I could do. I'm a junior programmer and don't yet possess the skill of debugging everything on my own
Thank you
EDIT:
here's the adapter:
class HousesAdapter(
private val houses: List<HouseItem>,
housesFragment: HousesFragment
) : RecyclerView.Adapter<HousesAdapter.HousesViewHolder>() {
override fun getItemCount() = houses.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
HousesViewHolder(
DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.recyclerview_house,
parent,
false
)
)
override fun onBindViewHolder(holder: HousesViewHolder, position: Int) {
holder.recyclerviewHouseBinding.house = houses[position]
}
inner class HousesViewHolder(
val recyclerviewHouseBinding: RecyclerviewHouseBinding
) : RecyclerView.ViewHolder(recyclerviewHouseBinding.root)
}
The fix:
Turns out I didn't put textviews inside the cardview. I totally forgot about that step and it was my mistake. Hopefully this post can serve as a reminder to others to populate their cardviews too and make sure it's filled. This'll also serve as a reminder for me. Very stupid mistake indeed, but now I'm sure I won't repeat it.
A big thanks to everyone who commented

