Я создаю приложение рецепта (статические данные).
Я создал класс "Рецепты", а внутри одноэлементного объекта я создал список экземпляров рецепта.
Класс Recipes содержит следующие параметры:
title: String
ingredients: List<String>
instructions : String
time: Long serves:
Int cost: String
source : String
Теперь я хочу создать адаптер для ингредиентов, чтобы я мог создать RecyclerView
для списка ингредиентов.
Myпроблема заключается в передаче адаптеру списка ингредиентов, а не списка рецептов.
Я пробую разные вещи, но не совсем уверен, что делать.Занимался этой проблемой уже несколько дней.
Это класс Рецептов:
class Recipes(val title: String, val ingredients: List<String>, val instructions: String, val time: Long, val serves: Int, val level: String, val source: String) {
private val prepTimeHours: Long = time / 60
private val preTimeMinutes: Long = time.rem(60)
val prepTime: String = "${prepTimeHours.toString()}:${preTimeMinutes.toString()}"
val peopleServed: String = "$serves adults"
}
Это синглтон:
object AllRecipes {
val recipeBook = listOf(
Recipes ("Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
Recipes ("2Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
Recipes ("3Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
Recipes ("4Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
Recipes ("5Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
Recipes ("6Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
Recipes ("7Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
Recipes ("8Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"))
}
Это мой текущийадаптер:
class IngredientsAdapter(val context: Context, val ingredient: List<Recipes>) : RecyclerView.Adapter<IngredientsAdapter.Holder>() {
inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val singleIngredient = itemView.findViewById<TextView>(R.id.single_ingredient)
fun bindText(textVar: Recipes, context: Context) {
singleIngredient.text = textVar.ingredients[adapterPosition]
}
}
override fun onBindViewHolder(holder: Holder, position: Int) {
holder.bindText(ingredient[position], context)
}
override fun getItemCount(): Int {
return ingredient.count()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.ingredients_layout, parent, false)
return Holder(view)
}
}
Любая помощь будет оценена!