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

标签 android list kotlin bundle mutable

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

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/

相关文章:

用于使用特定密码检查 id 的 PHP 代码

Android 导航 Controller 错误 `no current navigation code`

java - 在ListAdapter(ListView)中重新填充数据的最佳方法

Python读取.txt文件->列表

flutter - Flutter项目中的Application.kt提供错误-无法访问 'com.google.firebase.messaging.zzf'

android - Android 上的 Scala : best strategy for avoiding global data?

Android - 在 HTTP 请求之前刷新 ListView

java - 如何从另一个列表中替换对象列表中的元素

kotlin - Kotlin 中 setOf 和 hashSetOf 的区别

android - Dagger Hilt 如何将类注入(inject)到 ViewModel 中