如何在kotlin中的网格视图和列表视图之间使用setOnclickListener

时间:2017-09-20 03:32:03

标签: android android-activity arraylist kotlin baseadapter

我有3个活动连接:

网格视图的第一个活动" MainActivity.kt ", 列表视图的第二个" Main2Activity.kt ", 细节第三, 通过在网格视图活动中使用setOnclickListener我想点击图片(a)里面" MainActivity.kt "要在第二个活动中转换为adapter1" Main2Activity.kt ", 然后点击图片(b)" MainActivity.kt "要在第二个活动" Main2Activity.kt "中转换为adapter2,如何使其有效?

注意:我正在使用BaseAdapter 第二个活动包含2个ArrayList

以下是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 {
            if (foodView.ivFoodImage.equals(a)) {
            val intent = Intent(context, Main2Activity::class.java)

            intent.putExtra("name", food.name!!)
            intent.putExtra("des", food.des!!)
            intent.putExtra("image", food.image!!)
            context!!.startActivity(intent)
        }else if (foodView.ivFoodImage.equals(b)){
                val intent = Intent(context, Main2Activity::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 listOfFood[p0]
    }

    override fun getItemId(p0: Int): Long {
        return p0.toLong()
    }

    override fun getCount(): Int {

        return listOfFood.size
    }

}
}

这是Main2Activity.kt

class Main2Activity : AppCompatActivity() {
var adapter1: FoodAdapter? = null
var adapter2: FoodAdapter2? = null
var listOfFoods2 = ArrayList<Food>()
var listOfFoods3 = ArrayList<Food>()
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main2)

    val bundle = intent.extras
    val name = bundle.getString("name")
    val des = bundle.getString("des")
    val image = bundle.getInt("image")

    ivFoodImage2?.let {
        it.setImageResource(image)
    }
    //ivFoodImage2.setImageResource(image)
    tvName2?.let {
        it.text = name
    }
    //tvName2.text=name
    tvDes2?.let {
        it.text = des
    }
    //tvDes2.text=des



    // load foods2
    listOfFoods2.add(Food("Coffee", "   Coffee1 preparation is", R.drawable.a))
     listOfFoods2.add(Food("Coffee", "   Coffee2 preparation is", R.drawable.b))

         //listOfFoods2.add(Food("Coffee","   Coffee preparation is",R.drawable.c))
         //listOfFoods2.add(Food("Coffee","   Coffee preparation is",R.drawable.d))


    // load foods3


    //listOfFoods3.add(Food("Coffee","   Coffee preparation is",R.drawable.a))
    //listOfFoods3.add(Food("Coffee","   Coffee preparation is",R.drawable.b))
    listOfFoods3.add(Food("Coffee", "   Coffee3 preparation is", R.drawable.c))
    listOfFoods3.add(Food("Coffee", "   Coffee4 preparation is", R.drawable.d))



  if(adapter1 ==lvFoods2.adapter) {
  adapter1 = FoodAdapter(listOfFoods2, this)
  lvFoods2.adapter = adapter1
  } else if (adapter2==lvFoods2.adapter) {
  adapter2 = FoodAdapter2(listOfFoods3, this)
  lvFoods2.adapter = adapter2
  }

}


class FoodAdapter : 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(R.layout.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(R.layout.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
    }

}
}

0 个答案:

没有答案