Hello I have a list of foods that also contains the layout I want inflate when it is clicked.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val food = listOf(
Foods("Creme Brulee", "A rich egg dessert with torched sugar on top.", "French", R.drawable.cremebrulee1, R.layout.food_cremebrulee),
Foods("Hamburger", "Something something", "Spanish", R.drawable.person2, R.layout.food_hamburger),
Foods("Grilled Cheese", "Something something", "Chinese", R.drawable.person3, R.layout.food_hamburger),
Foods("French Onion Soup", "Something something", "French", R.drawable.person4, R.layout.food_hamburger),
Foods("Salmon", "Something something", "German", R.drawable.person2, R.layout.food_hamburger),
Foods("Fish Tacos", "Something something", "Dutch", R.drawable.person3, R.layout.food_hamburger),
Foods("Vegetable Sushi", "Something something", "Asian", R.drawable.person4, R.layout.food_hamburger),
Foods("Fried Egg", "Something something", "Thai", R.drawable.person1, R.layout.food_hamburger),
Foods("Vinagrette", "Thai style vinagrette with peanut butter", "Indian", R.drawable.person2, R.layout.food_hamburger)
)
myCustomFood.apply {
layoutManager = LinearLayoutManager(this@MainActivity)
adapter = FoodAdapter(food) {
d("marlon", "hi from MainActivity!")
//startActivity(Intent(this@MainActivity, FoodDetailView::class.java))
startActivity(Intent(this@MainActivity, FoodDetailView::class.java))
}
}
}
}
class FoodDetailView : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView() <--- What do I put here in order to grab the appropriate R.layout when a food on the list is clicked?
}
}
As you can see the first selection has R.layout.cremebrulee1 and the others are R.layout.hamburger, what do I put in setContentView in order to grab the R.layout from the list?
You can put your layout id in extras:
val intent = Intent(this@MainActivity, FoodDetailView::class.java)
intent.putExtra("EXTRA_LAYOUT", food.layout)
startActivity(intent)
And then get it back in you target activity:
val layoutId = intent.getIntExtra("EXTRA_LAYOUT", 0)
if (layoutId > 0) {
setContentView(layoutId)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With