'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
}
Solution 1:[1]
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
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dominik |
