Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android studio 如何从mainActivity访问变量(在协同程序中)?_Android Studio_Kotlin_Scope_Kotlin Coroutines - Fatal编程技术网

Android studio 如何从mainActivity访问变量(在协同程序中)?

Android studio 如何从mainActivity访问变量(在协同程序中)?,android-studio,kotlin,scope,kotlin-coroutines,Android Studio,Kotlin,Scope,Kotlin Coroutines,提前感谢您抽出时间 我正在练习API和网络调用,所以我建立了一个小程序,从公共API中获取一个玩家列表,并在recyclerView@MainActivity中显示。简单 问题是,除了从协同路由将列表发送到recyclerView适配器之外,我还希望能够将列表存储在某个位置,以便从协同路由外部访问它 这样我就可以在没有网络呼叫的情况下处理玩家列表 override fun onCreate(savedInstanceState: Bundle?) { super.onCr

提前感谢您抽出时间

我正在练习API和网络调用,所以我建立了一个小程序,从公共API中获取一个玩家列表,并在recyclerView@MainActivity中显示。简单

问题是,除了从协同路由将列表发送到recyclerView适配器之外,我还希望能够将列表存储在某个位置,以便从协同路由外部访问它

这样我就可以在没有网络呼叫的情况下处理玩家列表

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)

        CoroutineScope(Dispatchers.IO).launch {

            // Here we store the data received in a generic type "response"
            val response = LaRojaService
                .getLaRojaDataService()
                .getLaRojaPlayers()

            // Return to UI thread to process the data and inflate the recyclerView with it
            withContext(Dispatchers.Main) {
                if (response.isSuccessful) {
                    val temp = response.body()
                    val tempList = mutableListOf<Player>()

                    var index = 0
                    temp?.forEach {
                        val active = temp[index].active
                        val bio = temp[index].bio
                        val gamesPlayed = temp[index].gamesPlayed
                        val goalsScored = temp[index].goalsScored
                        val name = temp[index].name
                        val number = temp[index].number
                        val playerPictureUrl = temp[index].playerPictureUrl
                        val position = temp[index].position
                        val profilePictureUrl = temp[index].profilePictureUrl
                        val seasonsActive = temp[index].seasonsActive

                        tempList.add(Player(active, bio, gamesPlayed, goalsScored, name,
                            number, playerPictureUrl, position, profilePictureUrl, seasonsActive))

                        index += 1
                    }
                    recyclerView_home.layoutManager = LinearLayoutManager(this@HomeActvity)
                    recyclerView_home.adapter = HomePlayerAdapter(tempList)
                }
            }
        }
    }
我需要它做什么:

- Get a list of players from a public API using a coroutine
- Inflate the recyclerView with the list created on the MainThread
- Get a list of players from the API using the coroutine
- Save the list outside the coroutine (In this case, MainActivity)
- Work through the data inside the mainActivity
- Inflate the recyclerView (From Main activity)

再次非常感谢你。我对这个话题仍然很困惑。我甚至不知道这是否可能。

为什么不在
MainActivity
中声明一个属性呢?我不知道怎么做,我还在学习,我已经花了几个小时在这上面,所以这让我发疯:(你能举个例子吗?谢谢!也许可以看一下?我绝对建议在尝试应用程序之前先学习初学者Kotlin教程。它会让你的一切变得更简单、更快!我不建议你在理解属性、lambda和内联函数之前尝试学习协程。你现在的做法是这样的。)r coroutine打破了几个约定,但如果您不了解创建属性的相关知识,那么现在就无法解释所有这些约定。我知道如何创建属性,但我无法将从response.body获得的值分配给它