Reputation: 61
I have a problem to access a resource like a (Drawable by its name and not its int id)
In the MainActivity
class i can't access to the var drawableResourceId1
and var drawableResourceId2
inside the two if statement
, any solutions ?
Note 1:my first image named a
inside my Drawable and the second image named b
Note 2: i'm using multiple screens sizes to support all sizes inside the Drawable for a
and b
image like (ldpi
,mdpi
,hdpi
,xhdpi
,xxhdpi
,xxxhdpi
)
Here is a MainActivity.kt
class MainActivity : AppCompatActivity() {
var adapter:FoodAdapter?=null
var listOfFoods =ArrayList<Food>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(layout.activity_main)
// load foods
listOfFoods.add(Food("Coffee"," Coffee preparation is", a))
listOfFoods.add(Food("Coffee"," Coffee preparation is", b))
var drawableResourceId1 = this.resources.getIdentifier("a", "drawable", this.packageName)
var drawableResourceId2 = this.resources.getIdentifier("b", "drawable", this.packageName)
adapter= FoodAdapter(this,listOfFoods)
gvListFood.adapter =adapter
}
class FoodAdapter: BaseAdapter {
var listOfFood= ArrayList<Food>()
var context: Context?=null
constructor(context:Context,listOfFood:ArrayList<Food>):super(){
this.context=context
this.listOfFood=listOfFood
}
override fun getView(p0: Int, foodView: View?, p2: ViewGroup?): View? {
val food = this.listOfFood[p0]
var inflator = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
var foodView= inflator.inflate(layout.food_ticket, null)
foodView.ivFoodImage.setImageResource(food.image)
foodView.ivFoodImage.setOnClickListener {
val intent = Intent(context, Main2Activity::class.java)
if (foodView.ivFoodImage == drawableResourceId1){
intent.putExtra(Main2Activity.EXTRA_ADAPTER_MODE, AdapterType.ADAPTER_TYPE_1.ordinal)
intent.putExtra("name", food.name)
intent.putExtra("des", food.des)
intent.putExtra("image", food.image)
context!!.startActivity(intent)
}
if (foodView.ivFoodImage == drawableResourceId2) {
intent.putExtra(Main2Activity.EXTRA_ADAPTER_MODE, AdapterType.ADAPTER_TYPE_2.ordinal)
intent.putExtra("name", food.name)
intent.putExtra("des", food.des)
intent.putExtra("image", food.image)
context!!.startActivity(intent)
}
}
return foodView
}
override fun getItem(p0: Int): Any {
return listOfFood[p0]
}
override fun getItemId(p0: Int): Long {
return p0.toLong()
}
override fun getCount(): Int {
return listOfFood.size
}
}
}
Here is Food class
class Food (val name: String, val des: String, val image: Int)
Upvotes: 0
Views: 1488
Reputation: 2847
You can pass your values using construction
as you passing your Adapter
So, make some changes to your FoodAdapter class as mention below-:
class FoodAdapter: BaseAdapter {
var listOfFood= ArrayList<Food>()
var context: Context?=null
var d1:Int?=null
var d2:Int?=null
constructor(context:Context,listOfFood:ArrayList<Food>,drawableResourceId1:Int,drawableResourceId2:Int):super(){
this.context=context
this.listOfFood=listOfFood
this.d1 = drawableResourceId1
this.d2 = drawableResourceId2
}
override fun getView(p0: Int, foodView: View?, p2: ViewGroup?): View? {
val food = this.listOfFood[p0]
var inflator = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
var foodView= inflator.inflate(layout.food_ticket, null)
foodView.ivFoodImage.setImageResource(food.image)
foodView.ivFoodImage.setOnClickListener {
val intent = Intent(context, Main2Activity::class.java)
if (foodView.ivFoodImage == d1){
intent.putExtra(Main2Activity.EXTRA_ADAPTER_MODE, AdapterType.ADAPTER_TYPE_1.ordinal)
intent.putExtra("name", food.name)
intent.putExtra("des", food.des)
intent.putExtra("image", food.image)
context!!.startActivity(intent)
}
if (foodView.ivFoodImage == d2) {
intent.putExtra(Main2Activity.EXTRA_ADAPTER_MODE, AdapterType.ADAPTER_TYPE_2.ordinal)
intent.putExtra("name", food.name)
intent.putExtra("des", food.des)
intent.putExtra("image", food.image)
context!!.startActivity(intent)
}
}
return foodView
}
override fun getItem(p0: Int): Any {
return listOfFood[p0]
}
override fun getItemId(p0: Int): Long {
return p0.toLong()
}
override fun getCount(): Int {
return listOfFood.size
}
}
}
May Be, Help you
Upvotes: 1