程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何从 mainActivity 访问变量(在协程内)?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何从 mainActivity 访问变量(在协程内)??

开发过程中遇到如何从 mainActivity 访问变量(在协程内)?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何从 mainActivity 访问变量(在协程内)?的解决方法建议,希望对你解决如何从 mainActivity 访问变量(在协程内)?有所启发或帮助;

提前感谢您的时间!

我正在练习 API 和网络调用,因此我构建了一个小程序以从公共 API 获取玩家列表并将其显示在 recyclerVIEw @mainActivity 中。简单。

问题在于,除了将列表从协程发送到 recyclerVIEw Adapter 之外,我还希望能够将列表存储在某个地方,以便我可以从协程外部访问它。

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

    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)

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的如何从 mainActivity 访问变量(在协程内)?全部内容,希望文章能够帮你解决如何从 mainActivity 访问变量(在协程内)?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:如何从