Shao Fan
Shao Fan

Reputation: 37

What should I change if this is in a fragment?

The "this" in the two lines of this DailyMealFragment is having error. How could I change the "this" so that it could run in this DailyMealFragment

private fun loadRecyclerViewItems() {
        val linearLayoutManager = LinearLayoutManager(this)

        binding.itemsRv.layoutManager = linearLayoutManager

        val itemList:ArrayList<RestaurantsModels> = ArrayList()

        for (i in titles.indices){
            val model = RestaurantsModels(titles[i], descriptions[i], images[i])
            itemList.add(model)
        }

        val adapterItem = RestaurantsAdapter(this, itemList)
        binding.itemsRv.adapter = adapterItem
    }

Upvotes: 0

Views: 70

Answers (1)

Dominik
Dominik

Reputation: 109

LinearLayoutManager(requireContext()) do something like this. and for adapter create val adapterItem by lazy { RestaurantsAdapter() } on the top of your adapter class after imports

private fun loadRecyclerViewItems() {
    val linearLayoutManager = LinearLayoutManager(requireContext())

    binding.itemsRv.layoutManager = linearLayoutManager

    val itemList:ArrayList<RestaurantsModels> = ArrayList()

    for (i in titles.indices){
        val model = RestaurantsModels(titles[i], descriptions[i], images[i])
        itemList.add(model)
    }
    binding.itemsRv.adapter = adapterItem
}

Upvotes: 2

Related Questions