预期的类型与Kotlin中的if表达式不匹配

时间:2017-10-09 04:52:11

标签: android image kotlin

我想知道是否有人可以帮助我使用Kotlin解决if表达式中不匹配类型的问题?

我有2个活动连接: 第一个活动是MainActivity.kt,第二个活动是Main2Activity.kt,在第一个活动MainActivity class内我遇到mismatched types的问题,因为我使用if语句来选择要转换为第二个活动中的specific image specific adapter,例如点击图片(a)以转换为带ADAPTER_TYPE_1的第二个活动和图片{{1}转换为b

的第二个活动

注意:图片类型为ADAPTER_TYPE_2,我的Int内的第一张图片名为a,第二张图片名为Drawable

以下是MainActivity.kt

b

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))


    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 == a){
                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 == b) {
                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
    }

}
}

2 个答案:

答案 0 :(得分:2)

尝试用kotlin方式思考,即尽量不使用可空类型。你的课程也是用java风格编写的,但它可以用更简单的风格编写: class Food(val name: String, val des: String, val image: Int)

当你经常覆盖java方法时,你可以删除&#39;?&#39;从参数类型中删除&#39; !!&#39;在将来需要非null类型的时刻。 var class属性是不可取的,因为它给出了你需要注意运行时没有任何反应的状态。

上述提示可能会解决您的问题。 如果不是,您能否提供有关此错误的更多信息?(例如错误行号)

答案 1 :(得分:1)

您无法获取添加到图像视图中的drawable的资源ID。要指定此项,您可以使用 id 或甚至更好的标记。在您的情况下,您甚至可以在 food 实体中获得此信息,并且应该在此处使用它。

class FoodAdapter(
    val context: Context,
    val listOfFood: ArrayList<Food>
): BaseAdapter {

    override fun getView(p0: Int, foodView: View?, p2: ViewGroup?): View? {
        val food = this.listOfFood[p0]
        val inflator = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

        return inflator.inflate(layout.food_ticket, null).also {
            it.ivFoodImage.imageResource = food.image
            it.ivFoodImage.setOnClickListener { view ->

                context.startActivity(Intent(context, Main2Activity::class.java)
                    .putExtra("name", food.name)
                    .putExtra("des", food.des)
                    .putExtra("image", food.image)
                    .also {
                        when (food.image) {
                            a -> it.putExtra(Main2Activity.EXTRA_ADAPTER_MODE, AdapterType.ADAPTER_TYPE_1.ordinal)
                            b -> it.putExtra(Main2Activity.EXTRA_ADAPTER_MODE, AdapterType.ADAPTER_TYPE_2.ordinal)
                            else -> it
                        }
                    })
            }
        }
    }

    override fun getItem(p0: Int) = listOfFood[p0]

    override fun getItemId(p0: Int) = p0.toLong()

    override fun getCount() = listOfFood.size
}