kotlin 如何从协程中导出变量?

gcuhipw9  于 2022-11-16  发布在  Kotlin
关注(0)|答案(1)|浏览(145)

我是初学者,抱歉问了这么蠢的问题
我可以很容易地将数据从协程输出到View元素(例如,在TextView中),但是我不能将其保存在变量中。
p.s,前提是首先将其保存到TextView,然后从此处获取数据:
它不起作用:

lateinit var bind: ActivityMainBinding
val scope = CoroutineScope(Dispatchers.Default)

var name2 = ""
var race2 = ""
var dexterity2 = ""
var wisdom2 = ""
var strength2 = ""

fun expData() {
    scope.launch {
        val apiData = URL(CHARACTER_DATA_API).readText()
        withContext(Dispatchers.Main) {
            var spl = apiData.split(",")
            
**Log.d("MyTag","spl:${spl}")**
            
            name2 = spl[0]
            race2 = spl[1]
            dexterity2 = spl[2]
            wisdom2 = spl[3]
            strength2 = spl[4]
            
**Log.d("MyTag","spl val:${spl[0]},${spl[1]},${spl[2]},${spl[3]},${spl[4]}")**
        }
    }
}

        bind.butGenerate.setOnClickListener {
            expData()

**Log.d("MyTag","names var: ${name2},${race2},${dexterity2},${wisdom2},${strength2}")**
            }

日志:

1.【半身人,天堂面包师,15,17,7】
1.半身人,天堂面包师,15,17,7
1.名称变量:、、、、
我第二天受苦了,我找不到答案。怎么做才是正确的。请帮助!(Т_Т)

它工作

lateinit var bind: ActivityMainBinding
val scope = CoroutineScope(Dispatchers.Default)

fun fetchCharacterData(bind: ActivityMainBinding){
    scope.launch {
        val apiData = URL(CHARACTER_DATA_API).readText()
        withContext(Dispatchers.Main) {
            val spl = apiData.split(",")
            bind.run {
                txtName.text = spl[0]
                txtRace.text = spl[1]
                txtDexterity.text = spl[2]
                txtWisdom.text = spl[3]
                txtStrength.text = spl[4]

        }
    }
}

而且很管用

lateinit var bind: ActivityMainBinding
val scope = CoroutineScope(Dispatchers.Default)

fun fetchCharacterData(bind: ActivityMainBinding){
    scope.launch {
        val apiData = URL(CHARACTER_DATA_API).readText()
        withContext(Dispatchers.Main) {
            val spl = apiData.split(",")
            bind.run {
                txtName.text = spl[0]
                txtRace.text = spl[1]
                txtDexterity.text = spl[2]
                txtWisdom.text = spl[3]
                txtStrength.text = spl[4]

        }
    }
}

val name = bind.txtName.text
val race = bind.txtRace.text
val dex = bind.txtDextirity.text
val wis = txtWisdom.text
val str = txtStrength.text

但在我看来,这是非常长和不正确

现在它'工作=)

lateinit var bind: ActivityMainBinding
val scope = CoroutineScope(Dispatchers.Default)

var name2 = ""
var race2 = ""
var dexterity2 = ""
var wisdom2 = ""
var strength2 = ""

fun expData() {
    scope.launch {
        val apiData = URL(CHARACTER_DATA_API).readText()
        withContext(Dispatchers.Main) {
            var spl = apiData.split(",")
            
**Log.d("MyTag","spl:${spl}")**
            
            name2 = spl[0]
            race2 = spl[1]
            dexterity2 = spl[2]
            wisdom2 = spl[3]
            strength2 = spl[4]
            
**Log.d("MyTag","spl val:${spl[0]},${spl[1]},${spl[2]},${spl[3]},${spl[4]}")**
        }
    }
}

        bind.butGenerate.setOnClickListener {
            expData()scope.launch {
        withContext(Dispatchers.Main) {
            while (name.isBlank()) {
                delay(20)
            }
**Log.d("MyTag","names var: ${name2},${race2},${dexterity2},${wisdom2},${strength2}")**
            }
bprjcwpo

bprjcwpo1#

在下列程式码中:

bind.butGenerate.setOnClickListener {
    expData()

    **Log.d("MyTag","names var: ${name2},${race2},${dexterity2},${wisdom2},${strength2}")**
}

expData()方法中启动一个协程,然后记录结果。这是行不通的,因为协程是异步的,到达log语句时变量将为空。
如果你想在变量初始化后执行一些代码,你需要在协程块中执行:

bind.butGenerate.setOnClickListener {
    scope.launch {
        val apiData = URL(CHARACTER_DATA_API).readText()
        withContext(Dispatchers.Main) {
            var spl = apiData.split(",")
                    
            name2 = spl[0]
            race2 = spl[1]
            dexterity2 = spl[2]
            wisdom2 = spl[3]
            strength2 = spl[4]
        }

        // ... EXECUTE YOUR CODE HERE
        // ALL VARIABLES WILL BE INITIALIZED HERE

        Log.d("MyTag","names var: ${name2},${race2},${dexterity2},${wisdom2},${strength2}")
    }
}

相关问题