如何将可变列表传递给捆绑包?

tor*_*ore 5 android bundle list mutable kotlin

我想将可变列表添加到捆绑包中,但是似乎没有一种方法可以完成此操作。

var bundle = Bundle()
                bundle.put...????("LIST", fbModel.recipeArray)
Run Code Online (Sandbox Code Playgroud)

您可以使用putString等等,但似乎没有putMutableList选择。该怎么办?

更新 忘了提到mutableListrecipeArray是一个object。像这样:

var recipeArray: MutableList<RecipeTemplate>
Run Code Online (Sandbox Code Playgroud)

...其中RecipeTemplate是一个看起来像这样的类:

class RecipeTemplate {
    var recipeHeader: String? = null
    var recipeText: String? = null
    var recipeImage: String? = null
    var recipeKey: String? = null
}
Run Code Online (Sandbox Code Playgroud)

更新 根据@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)
            }
Run Code Online (Sandbox Code Playgroud)

但是我在其他活动中仍然无法接收它。来自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

}
Run Code Online (Sandbox Code Playgroud)

counter传递和正确的数字显示。在recipeArray.size传递和显示正确的号码。但是,其他日志recipeArray[1].recipeHeader.toString()recipeList[1].recipeHeader.toString()均为空,即使它们在放入束中之前包含正确的值。我需要做些什么...枚举...解析列表?还是我错过了什么?

zsm*_*b13 6

这取决于您列表中的项目。Bundle有几个方法可以让您在其中放置列表,例如putIntegerArrayListputStringArrayList。如果您需要使用自定义类型,您可以制作Parcelable并使用putParcelableArrayList.

当然,正如其名称所示,所有这些方法都ArrayList专门采用实例,而不仅仅是任何MutableList. 因此,您可以更改所有MutableList用法来ArrayList代替使用,也可以在将其放入捆绑包时ArrayList从现有用法中创建一个新用法:MutableList

bundle.putParcelableArrayList("key", ArrayList(myParcelableList))
Run Code Online (Sandbox Code Playgroud)


Epi*_*rce 6

你可以做

apply plugin: 'kotlin-android-extensions' 

androidExtensions {
    experimental = true
}
Run Code Online (Sandbox Code Playgroud)

然后

@Parcelize
class RecipeTemplate : Parcelable {
    var recipeHeader: String? = null
    var recipeText: String? = null
    var recipeImage: String? = null
    var recipeKey: String? = null
}
Run Code Online (Sandbox Code Playgroud)

然后

var bundle = Bundle().apply {
                  putParcelableArrayList("LIST", ArrayList<Parcelable>(fbModel.recipeArray))
             }
Run Code Online (Sandbox Code Playgroud)