0

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 = ""
)
help
  • 13
  • 4
  • One code improvement. Instead of `val sectorOption = findViewById(R.id.sectorOption) as Spinner` -> you can directly use `val sectorOption = findViewById(R.id.sectorOption)` – Natig Babayev Feb 24 '20 at 14:24
  • You will have to make your data class [Parcelable](https://developer.android.com/reference/android/os/Parcelable). see [this](https://stackoverflow.com/questions/2736389/how-to-pass-an-object-from-one-activity-to-another-on-android) and [this](https://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents) – mightyWOZ Feb 24 '20 at 14:26
  • Does this answer your question? [How to send an object from one Android Activity to another using Intents?](https://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents) – mightyWOZ Feb 24 '20 at 14:26
  • @mightyWOZ Thanks for the links to parcelable and serializable but I am not able to implement those in Kotlin as I can't find any reliable documentation or solution for that since most documentation and answers are in Java. – help Feb 24 '20 at 14:35

1 Answers1

0

One approach is to use @Parcelable annotation from Android Kotlin extensions plugin.

It saves you writing boilerplate code and provides a way to customize serialization/deserialization via implementing Parceler instances.

Also you still have the option to implement the Parcelable interface the normal way with Kotlin: https://developer.android.com/reference/kotlin/android/os/Parcelable

Mina Wissa
  • 10,923
  • 13
  • 90
  • 158