I have a listview implementation(in HomeActivity.kt) of user objects displaying specific data of the users from an ArrayList. My setOnItemClickListener works fine and takes the user to another activity(DetailsActivity.kt) when any item is selected. I want to pass all the information of the selected user to display it in the DetailsActicity.kt. How do I implement Parcelable in KOTLIN to do this?
Here is my HomeActivity.kt
val sectors = arrayOf("Sector", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
val sectorOption = findViewById<View>(R.id.sectorOption) as Spinner
if(sectorOption != null){
val sectorAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, sectors)
sectorOption.adapter = sectorAdapter
sectorOption.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
if (parent != null) {
sect = parent.getItemAtPosition(position).toString()
db.collection("Users").whereEqualTo("sector", sect)
.get()
.addOnCompleteListener { task ->
if(task.isSuccessful){
UserList.clear()
for(document in task.result!!){
Log.d(ContentValues.TAG, "test" + document.id + "=>" + document.data)
UserList.add(User(document.get("Name").toString(), document.get("EmailID").toString(), document.get("sector").toString()))
}
UserDataAdapter = UserDataAdapter(ArrayList(UserList), applicationContext)
listview.setAdapter(UserDataAdapter)
}
}
}
}
override fun onNothingSelected(parent: AdapterView<*>?) {
}
}
}
listview.setOnItemClickListener{parent, view, position, id ->
val element = UserDataAdapter?.getItem(position)
val intent = Intent(this, OrderActivity::class.java)
startActivity(intent)
}
And this is my User.kt data class
data class User(val Name: String = "",
val sector: String = "",
val Address: String = "",
val Mobile: String = "",
val EmailID: String = ""
)