许多listview和每个包含Kotlin不同的对象

我有2个活动,当点击第一个活动被转换为第二个活动,这是Main2Activity

里面的第二个活动我试图使许多ListView和每个包含图片和文本,每个ListView里面一个特定的适配器,但我得到一个问题代表( adapterType1adapterType2 )两个适配器( adapterType1adapterType2 )返回相同的值( adapterType1 ),我想返回两个不同的结果( adapterType1 )和( adapterType2 )?

这是一个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) 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!!1 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 } } } 

这是一个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 (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 } } 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, p1: View?, p2: ViewGroup?): View { val food = this.listOfFoodsLocal2[p0] var inflator = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater val foodView = inflator.inflate(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(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 } } } 

这里是activitymain2.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" 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> 

这是ticket2.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="match_parent" android:layout_height="match_parent" android:background="@color/gray" android:orientation="vertical" android:padding="3pt"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/background" android:orientation="horizontal"> <ImageView android:id="@+id/ivFoodImage2" android:layout_width="50pt" android:layout_height="50pt" android:layout_weight="1" app:srcCompat="@drawable/a" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" android:padding="2pt"> <TextView android:id="@+id/tvName2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Coffee" android:textSize="18sp" /> <TextView android:id="@+id/tvDes2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Could be prepare with suger" /> </LinearLayout> </LinearLayout> </LinearLayout>