Reputation: 61
I wonder if somebody can help me to fix this issue with mismatched types inside the if expression by using Kotlin?
I have 2 activities to connect between the:
The first activity is MainActivity.kt
and the second activity is Main2Activity.kt
, inside first activity MainActivity class
i got a problem for mismatched types
, because i'm using if statement to choose specific image
to be convert to the specific adapter
in the second activity like when click on image (a
) to be converted to the 2nd activity with ADAPTER_TYPE_1
and image (b
) converted to the 2nd activity with ADAPTER_TYPE_2
Note: image type is Int
, my first image named a
inside my Drawable
and the second image named b
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))
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
}
}
}
Here is Food class
class Food(val name: String, val des: String, val image: Int)
Upvotes: 1
Views: 2257
Reputation: 39843
You cannot get the resource id of the drawable you added to the image view. For specifying this you can use the id or even better the tag. In you case you even have this information with your food entity and should use it here.
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
}
Upvotes: 1
Reputation: 78
Try to think in kotlin-way, i.e. try to not use nullable types. Also your class Food written in java-style but it can be written in more simple style:
class Food(val name: String, val des: String, val image: Int)
When you override java methods often you can remove '?' from parameter type in order to throw away '!!' in future moments when requires not-null type. var class property is not desirable because it gives state that you need to watch that nothing happens in runtime.
Probably tips above will resolve your issues. If no, can you give more information about this error?(e.g. line number of error)
Upvotes: 2