I want to add a mutable list to a bundle, but there doesn't seem to be a way to accomplish this.
var bundle = Bundle()
bundle.put...????("LIST", fbModel.recipeArray)
You can use putString
and so on, but there doesn't seem to be a putMutableList
as an option. What to do?
UPDATE
Forgot to mention that the mutableList
recipeArray is an object
. Like this:
var recipeArray: MutableList<RecipeTemplate>
...where RecipeTemplate is a class that looks like this:
class RecipeTemplate {
var recipeHeader: String? = null
var recipeText: String? = null
var recipeImage: String? = null
var recipeKey: String? = null
}
UPDATE Solved issue according to answer by @EpicPandaForce:
gridView.setOnItemClickListener { parent, view, position, id ->
Log.d("TAGA", "CLICKETICLICK " + position)
Log.d("TAGA", "CLICKETICLICK " + fbModel.recipeArray[1].recipeHeader) //PASSES
val intent = Intent(classContext, Recipes::class.java)
var bundle = Bundle().apply {
putParcelableArrayList("LIST", ArrayList<Parcelable>(fbModel.recipeArray))
putInt("POSITION", position)
}
intent.putExtra("bundle", bundle)
startActivity(intent)
}
But I'm still having problem receiving it in the other activity. Code here from onCreate:
var passedIntent = intent.extras
var bundle: Bundle = passedIntent.getBundle("bundle")
var counter: Int = bundle.getInt("POSITION", 0)
var recipeArray: ArrayList<Parcelable> = bundle.getParcelableArrayList("LIST")
var recipeList: MutableList<RecipeTemplate> = recipeArray as MutableList<RecipeTemplate>
Log.d("TAGA", "PASSED " + counter) //PASSES
if(recipeArray != null) {
Log.d("TAGA", "HERE " + recipeArray[1].recipeHeader.toString()) //Null
Log.d("TAGA", "HERE " + recipeList[1].recipeHeader.toString()) //Null
Log.d("TAGA", "HERE " + recipeArray.size) //PASSES
}
The counter
is passed and the correct number is shown. The recipeArray.size
is passed and shows the correct number. However, the other logs recipeArray[1].recipeHeader.toString()
and recipeList[1].recipeHeader.toString()
are both null even though they contain the correct values before being put in the bundle. Is there something I need to do to... eum... de-parse the list? Or am I missing something?
To add an element to a Mutable List in Kotlin, we can use add(element), or add(index, element) functions. add(element) adds element to the end of this Mutable List. add(index, element) adds element to this Mutable List at the given index.
A quick glance at Kotlin's documentation provides a good overview about what the difference between them is: while List provides read-only access to stored elements, MutableList provides “list-specific write operations” that allow us to add or remove particular elements from an existing list.
But lately in Kotlin all you have to do is simple. First, in your data class you implement Serializable. Then in your source Activity, you add it to your bundle and cast it as Serializable. In your destination Activity, you retrieved the list.
These are some important points you should know before working with Kotlin MutableList: List is read-only (immutable), you cannot add or update items in the original list. MutableList inherites List and supports read/write access, you can add, update or remove items.
You can do
//apply plugin: 'kotlin-android-extensions'
//androidExtensions {
// experimental = true
//}
apply plugin: 'kotlin-parcelize'
Then
@Parcelize
class RecipeTemplate : Parcelable {
var recipeHeader: String? = null
var recipeText: String? = null
var recipeImage: String? = null
var recipeKey: String? = null
}
Then
var bundle = Bundle().apply {
putParcelableArrayList("LIST", ArrayList<Parcelable>(fbModel.recipeArray))
}
This depends on what items you have in your list. Bundle
has a couple methods that allow you to place lists in it, for example putIntegerArrayList
and putStringArrayList
. If you need to use a custom type, you can make it Parcelable
and use putParcelableArrayList
.
Of course, all these methods as their name suggest take ArrayList
instances specifically, not just any MutableList
. So you can either change all your MutableList
usages to use ArrayList
instead, or you can create a new ArrayList
from your existing MutableList
when you're putting it in the bundle:
bundle.putParcelableArrayList("key", ArrayList(myParcelableList))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With