many listview and each one contains different objects in Kotlin

送分小仙女□ 提交于 2019-12-13 03:53:38

问题


I have 2 activity and when click on first activity to be converted to second activity it's Main2Activity

inside 2nd activity I am trying to make many ListView and each one contains pictures and text, each ListView inside one specific adapter but i get a problem for represent (adapterType1,adapterType2) both adapters (adapterType1,adapterType2) return the same value of (adapterType1),and i wanna to return two different result for (adapterType1) and (adapterType2)?

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

}
}

Here is a 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
    }

}
}

Here is 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>

Here is 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>

来源:https://stackoverflow.com/questions/46536953/many-listview-and-each-one-contains-different-objects-in-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!