Kotlin中无法访问的可绘制图像名称?

时间:2017-10-12 05:21:28

标签: android image kotlin

我有一个问题,就是访问资源,比如(通过名称而不是它的int id)

MainActivity课程中,我无法访问两个drawableResourceId1内的var drawableResourceId2和var if statement,是否有任何解决方案?

注1:我的第一张图像在我的Drawable中名为a,第二张图像名为b

注意2:我使用多种屏幕尺寸来支持Drawable中所有尺寸的ab图片,如ldpimdpi,{ {1}},hdpixhdpixxhdpi

以下是MainActivity.kt

xxxhdpi

error image

这是食品类

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
    }

}
}

1 个答案:

答案 0 :(得分:1)

您可以在传递construction

时使用Adapter传递值

因此,如下所述,对 FoodAdapter 类进行一些更改 -

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
    }

   }
 }

可以,帮助你