我想知道是否有人可以帮我解决代表When Expression
特定代码块的问题?
我有两个活动要连接:
第一个活动是MainActivity.kt
,第二个活动是Main2Activity.kt
,在第二个活动中,我遇到问题,因为在Main2Activity class
内部使用特定代码块时应用表达式
因为我想在点击Main2Activity class
中的图像时点击MainActivity class
内的特定适配器,例如在点击第一个活动类中的图片ADAPTER_TYPE_2
时传递b
,并且主要问题是始终在Main2Activity class
注意:第一个活动使用GridView
,第二个活动使用ListView
,我在Main2Activity.kt
内有2个适配器,我使用enum
包含第二个适配器活动
以下是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
}
}
}
这是第一项活动的food_ticket.xml
<?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>
这是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
<?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
enum class AdapterType {
ADAPTER_TYPE_1,
ADAPTER_TYPE_2;
companion object {
fun fromOrdinal(ordinal: Int): AdapterType? {
return AdapterType.values().firstOrNull { it.ordinal == ordinal }
}
}
}
答案 0 :(得分:1)
实施已关闭
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
}
adapter1
变量为空以及lvFoods2.adapter
。根据我对代码的理解,它应该如下所示:
lvFoods2.adapter = when (type) {
ADAPTER_TYPE_1 -> FoodAdapter1(listOfFoods2, this)
ADAPTER_TYPE_2 -> FoodAdapter2(listOfFoods3, this)
else -> lvFoods2.adapter
}