gpt4 book ai didi

android - 如何将可变列表传递给 bundle ?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:26:02 26 4
gpt4 key购买 nike

我想将一个可变列表添加到一个包中,但似乎没有办法实现这一点。

var bundle = Bundle()
bundle.put...????("LIST", fbModel.recipeArray)

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

更新忘记提及 mutableList recipeArray 是一个对象。像这样:

var recipeArray: MutableList<RecipeTemplate>

...其中 RecipeTemplate 是一个如下所示的类:

class RecipeTemplate {
var recipeHeader: String? = null
var recipeText: String? = null
var recipeImage: String? = null
var recipeKey: String? = null
}

更新根据@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)
}

但我在其他 Activity 中接收它时仍然遇到问题。来自 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

}

计数器 被传递并显示正确的数字。 recipeArray.size 已传递并显示正确的数字。但是,其他日志 recipeArray[1].recipeHeader.toString()recipeList[1].recipeHeader.toString() 都是空的,即使它们包含正确的值在放入 bundle 之前。有什么我需要做的......嗯......取消解析列表?还是我遗漏了什么?

最佳答案

你可以做到

//apply plugin: 'kotlin-android-extensions' 

//androidExtensions {
// experimental = true
//}

apply plugin: 'kotlin-parcelize'

然后

@Parcelize
class RecipeTemplate : Parcelable {
var recipeHeader: String? = null
var recipeText: String? = null
var recipeImage: String? = null
var recipeKey: String? = null
}

然后

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

关于android - 如何将可变列表传递给 bundle ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51130813/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com