简体   繁体   English

将 Parcelable arraylist 从 MainActivity 传递给 Fragment

[英]Pass parcelable arraylist from MainActivity to Fragment

I want to pass an arraylist of objects from my MainActivity to my OverviewFragment.我想将对象的数组列表从我的 MainActivity 传递到我的 OverviewFragment。 The class is as follows.类如下。

@Parcelize
data class User(
    val id: String? = null,
    val firstName: String? = null,
    val lastName: String? = null,
    val email: String? = null,
    val WSC_Member: Boolean? = false,
    val Landesliga: Boolean? = false,
    val Superuser: Boolean? = false
) : Parcelable {

    fun getName(): String? {
        return "$firstName $lastName"
    }

}

Inside the MainActivity's onCreateView I get all users via the function getUsersData and pass it to the bundle在 MainActivity 的onCreateView我通过函数getUsersData获取所有用户并将其传递给包

val overviewFragment: Fragment = OverviewFragment()
val fragmentManager: FragmentManager = supportFragmentManager
val data = Bundle()

// Get all users
GlobalScope.launch(Dispatchers.IO) {

    val allPlayers = getUsersData(userDocs)
    data.putParcelableArrayList("allPlayers", allPlayers)
    overviewFragment.arguments = data

    fragmentManager.beginTransaction().replace(
        R.id.overviewLayout,
        overviewFragment,
        "overviewFragment"
    ).addToBackStack(null).commit()

}

The getUsersData function: getUsersData函数:

suspend fun getUsersData(UserCollection: CollectionReference): ArrayList<User> {

    val snapshot = UserCollection.get().await()
    val allPlayers = arrayListOf<User>()

    snapshot.documents.forEach { playerI ->

        val userI = User(
            playerI.id,
            playerI.getString("firstName"),
            playerI.getString("lastName"),
            playerI.getString("email"),
            playerI.getBoolean("WSC_Member"),
            playerI.getBoolean("Landesliga"),
            playerI.getBoolean("Superuser")
        )

        allPlayers.add(userI)

    }

    return allPlayers

} }

In the OverviewFragments onCreateView在OverviewFragments onCreateView

    val data = arguments
    val allPlayers = data?.getParcelableArrayList<User>("allPlayers")
    val test1 = allPlayers?.count()
    Log.i(TAG, "Test1: $test1")

I try to get the data, but it stays null .我尝试获取数据,但它保持为null What am I missing?我错过了什么?

I/OverviewFragment: Test1: null

This is not what you want.这不是你想要的。 You shouldn't be passing this data between screens manually, instead it should be stored in a Repository, that is accessible to each screen in the app, and they can pull the data themselves.您不应该手动在屏幕之间传递这些数据,而应该将其存储在存储库中,应用程序中的每个屏幕都可以访问该存储库,并且他们可以自己提取数据。 You could be passing an Id as argument to the Fragment, if you wanted it to pull data for a specific item.如果您希望它为特定项目提取数据,您可以将 Id 作为参数传递给 Fragment。

Also, you're incorrectly using coroutines, they're not meant to be used from the Activity, and definitely not with the GlobalScope.此外,您错误地使用了协程,它们不应该在 Activity 中使用,绝对不能与 GlobalScope 一起使用。 You should use a ViewModel, which has a viewModelScope, whose lifecycle is tied to the lifecycle of the screen.您应该使用具有 viewModelScope 的 ViewModel,其生命周期与屏幕的生命周期相关联。 The ViewModel also survives orientation changes. ViewModel 也能经受住方向变化。 You'd access your Repository from the ViewModel, and the Fragment would then observe the data by using LiveData.您将从 ViewModel 访问您的存储库,然后 Fragment 将使用 LiveData 观察数据。

You can check out Google's Sunflower sample project that showcases these patterns.您可以查看展示这些模式的 Google 的Sunflower示例项目。

For more information, check out the following links:有关更多信息,请查看以下链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM