Cuando la expresión en kotlin no funciona como se esperaba

Me pregunto si alguien me puede ayudar a resolver este problema para representar el bloque de código específico de When Expression .

Tengo 2 actividades para conectarme entre:

La primera actividad es MainActivity.kt y la segunda actividad es Main2Activity.kt , en la segunda actividad tengo un problema para aplicar When Expression con un bloque específico de código dentro de la Main2Activity class porque quiero pasar el adaptador específico dentro de la Main2Activity class cuando Main2Activity class clic en la image en el MainActivity class como pasar ADAPTER_TYPE_2 cuando se hace clic en la image b en la primera class de actividad, y el problema principal de eso siempre es Aplicar el primer bloque de código dentro de la Main2Activity class

Nota: la primera actividad usó GridView y la segunda actividad usó ListView y tengo 2 adaptadores dentro de Main2Activity.kt y estoy usando enum consiste en cada adaptador en la segunda actividad

Aquí hay una 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)) listOfFoods.add(Food("Coffee"," Coffee preparation is", c)) listOfFoods.add(Food("Coffee"," Coffee preparation is", d)) 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, p1: 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) when (foodView) { foodView.ivFoodImage.also { 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) } foodView.ivFoodImage.also { 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 } } } 

Aquí está food_ticket.xml para la primera actividad

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="63pt" android:layout_height="wrap_content" android:background="@color/gray" android:orientation="vertical" android:padding="3pt"> <LinearLayout android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background" android:orientation="vertical"> <ImageView android:id="@+id/ivFoodImage" android:layout_width="50pt" android:layout_height="50pt" app:srcCompat="@drawable/c" /> <TextView android:id="@+id/tvName" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="Coffe" android:textSize="20sp" /> </LinearLayout> </LinearLayout> 

Aquí hay una Main2Activity.kt

 class Main2Activity : AppCompatActivity() { companion object { val EXTRA_ADAPTER_MODE = "extra_adapter_mode" } var adapter1: FoodAdapter1?= null var adapter2: FoodAdapter2? = null var listOfFoods2 = ArrayList<Food>() var listOfFoods3 = ArrayList<Food>() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(activity_main2) val bundle = intent.extras val adapterTypeOrdinal = intent.getIntExtra(EXTRA_ADAPTER_MODE, 0) // 0 is the default value val type = AdapterType.fromOrdinal(ordinal = adapterTypeOrdinal) val name = bundle.getString("name") val des = bundle.getString("des") val image = bundle.getInt("image") ivFoodImage2?.let { it.setImageResource(image) } tvName2?.let { it.text = name } tvDes2?.let { it.text = des } // load foods2 listOfFoods2.add(Food("Coffee", " Coffee1 preparation is", R.drawable.a)) listOfFoods2.add(Food("Coffee", " Coffee2 preparation is", R.drawable.b)) // load foods3 listOfFoods3.add(Food("Coffee", " Coffee3 preparation is", R.drawable.c)) listOfFoods3.add(Food("Coffee", " Coffee4 preparation is", R.drawable.d)) //var ADAPTER_TYPE_1 = adapter1 //var ADAPTER_TYPE_2 = adapter2 lvFoods2.adapter = when (type) { AdapterType.ADAPTER_TYPE_1-> FoodAdapter1(listOfFoods2, this) AdapterType.ADAPTER_TYPE_2-> FoodAdapter2(listOfFoods3, this) else -> lvFoods2.adapter } } class FoodAdapter1 : BaseAdapter{ var context: Context? = null var listOfFoodsLocal2 = ArrayList<Food>() constructor(listOfFoods2: ArrayList<Food>, context: Context) : super() { this.listOfFoodsLocal2 = listOfFoods2 this.context = context } override fun getView(p0: Int, foodView: View?, p2: ViewGroup?): View { val food = this.listOfFoodsLocal2[p0] var inflator = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater val foodView = inflator.inflate(food_ticket2, null) foodView.ivFoodImage2?.let { it.setImageResource(food.image!!) } foodView.tvName2?.let { it.text = food.name!! } foodView.tvDes2?.let { it.text = food.des!! } foodView.ivFoodImage2.setOnClickListener { //move to next val intent = Intent(context, FoodDetails::class.java) 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 listOfFoodsLocal2[p0] } override fun getItemId(p0: Int): Long { return p0.toLong() } override fun getCount(): Int { return listOfFoodsLocal2.size } } class FoodAdapter2 : BaseAdapter { var context: Context? = null var listOfFoodsLocal3 = ArrayList<Food>() constructor(listOfFoods3: ArrayList<Food>, context: Context) : super() { this.listOfFoodsLocal3 = listOfFoods3 this.context = context } override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View { val food = this.listOfFoodsLocal3[p0] var inflator = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater val foodView = inflator.inflate(food_ticket2, null) foodView.ivFoodImage2?.let { it.setImageResource(food.image!!) } foodView.tvName2?.let { it.text = food.name!! } foodView.tvDes2?.let { it.text = food.des!! } foodView.ivFoodImage2.setOnClickListener { //move to next val intent = Intent(context, FoodDetails::class.java) 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 listOfFoodsLocal3[p0] } override fun getItemId(p0: Int): Long { return p0.toLong() } override fun getCount(): Int { return listOfFoodsLocal3.size } } } 

activity_main2.xml para la 2da actividad

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/gray" android:orientation="vertical" tools:context="com.marzadmz.startup.Main2Activity"> <ListView android:id="@+id/lvFoods2" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/gray" /> </LinearLayout> 

AdapterType.kt para la class enum

 enum class AdapterType { ADAPTER_TYPE_1, ADAPTER_TYPE_2; companion object { fun fromOrdinal(ordinal: Int): AdapterType? { return AdapterType.values().firstOrNull { it.ordinal == ordinal } } } } 

image de error nulo 2da image

La implementación está apagada

 var ADAPTER_TYPE_1 = adapter1 var ADAPTER_TYPE_2 = adapter2 lvFoods2.adapter = when (lvFoods2.adapter) { ADAPTER_TYPE_1 -> FoodAdapter1(listOfFoods2, this).also { ADAPTER_TYPE_1 = it } ADAPTER_TYPE_2 -> FoodAdapter2(listOfFoods3, this).also { ADAPTER_TYPE_2 = it } else -> lvFoods2.adapter } 

La variable adapter1 es nula y también lvFoods2.adapter . Por lo que entiendo tu código, debería verse así:

 lvFoods2.adapter = when (type) { ADAPTER_TYPE_1 -> FoodAdapter1(listOfFoods2, this) ADAPTER_TYPE_2 -> FoodAdapter2(listOfFoods3, this) else -> lvFoods2.adapter } 
  • Problema de tipo de habitación de Android
  • ¿Por qué no encuentra Guice mi encuadernación List <Interface>?
  • El método RXjava2 en fromCallable no se puede exceder
  • Tipo de parámetro Kotlin no coincide
  • Cómo vincular el button con el website en el estudio de Android usando kotlin
  • ¿Insertar o agregar un elemento en ArrayList en Kotlin?
  • Kotlin Back-Tick escapando en nombres de methods: ¿Cómo funciona?
  • ¿Puedo escribir una function de extensión de Kotlin que use un bean de spring autoconectado?
  • Cobertura de Jacoco y parameters pnetworkingeterminados de Kotlin
  • java.lang.VerifyError creando una tarea de gradle con kotlin
  • Por qué Netbeans se queja de la ambigüedad, mientras que Intellij no lo hace