Search code examples
androidlistkotlinbundlemutable

How do I pass a mutable list to a bundle?


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?


Solution

  • 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))
                 }