Marzadmz Marzadmz - 3 years ago 194
Android Question

Applying enum consist of 3 adapters between 2 activities in Kotlin

I wonder if somebody can help me to solve enum problem for represent 2 object inside

Main2Activity.kt
?

I have 2 activities to connect between the:
The first activity for Grid View
MainActivity.kt
, second for List View
Main2Activity.kt
and I have 3 object instance (
adapter
) from
MainActivity.kt
and (
adapter1
,
adapter2
) from
Main2Activity.kt
and i wanna pass specific object instance when click on specific image in the
MainActivity.kt


Note: I'm using
BaseAdapter
.

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 {


if (foodView.ivFoodImage.context!!.equals(a) ) {
val intent = Intent(context, Main2Activity::class.java)
intent.putExtra(Main2Activity.EXTRA_ADAPTER_MODE, AdapterType.adapter1.ordinal)
intent.putExtra("name", food.name!!)
intent.putExtra("des", food.des!!)
intent.putExtra("image", food.image!!)

context!!.startActivity(intent)

}
else if (foodView.ivFoodImage.context!!.equals(b)) {
val intent = Intent(context, Main2Activity::class.java)
intent.putExtra(Main2Activity.EXTRA_ADAPTER_MODE, AdapterType.adapter2.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
}

}
}


Here is a Main2Activity.kt

class Main2Activity : AppCompatActivity() {
companion object {
val EXTRA_ADAPTER_MODE = "extra_adapter_mode"
}
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?.getIntExtra(EXTRA_ADAPTER_MODE, AdapterType.adapter.ordinal)
val adapterType = bundle?.let { AdapterType.fromOrdinal(it) }



// val bundle = intent.extras
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))

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

}
}


Here is enum class

enum class AdapterType {
adapter,
adapter1,
adapter2;


companion object {
fun fromOrdinal(ordinal: Int): AdapterType? {
return AdapterType.values().firstOrNull { it.ordinal == ordinal }
}
}
}


first activity image

adapters enum class

2nd activity image

Answer Source

You just have to send it in your bundle like the other informations

intent.putExtra(Main2Activity.EXTRA_ADAPTER_MODE, AdapterType.ADAPTER_TYPE_1.toOrdinal())

and then get it from the bundle in your second Activity

val adapterTypeOrdinal = bundle.getIntExtra(EXTRA_ADAPTER_MODE, 0) // 0 is the default value
val type = AdapterType.fromOrdinal(adapterTypeOrdinal)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download