11

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?

tore
  • 619
  • 2
  • 9
  • 23

3 Answers3

16

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))
             }
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
6

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))
zsmb13
  • 85,752
  • 11
  • 221
  • 226
0

What's the type of recipeArray? You can put various kinds of ArrayList (not List!), including Integers, CharSequences, Strings, and, most generally, Parcelables. See the docs for the full API: https://developer.android.com/reference/android/os/Bundle

AutonomousApps
  • 4,229
  • 4
  • 32
  • 42