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 如何知道线程何时完成执行_Android Studio_Kotlin_Runnable - Fatal编程技术网

Android studio 如何知道线程何时完成执行

Android studio 如何知道线程何时完成执行,android-studio,kotlin,runnable,Android Studio,Kotlin,Runnable,我正在Android studio中使用Kotlin做一个测验应用程序。我正在从一个API获取数据,并将其显示在活动中。现在我正在实现一个进度条,它指示回答每个问题的时间。这是我从API获取的函数: private fun fetch(){ val theme = intent.getIntExtra("sport", 21) val difficulty = intent.getStringExtra("medium") val url = "https://op

我正在Android studio中使用Kotlin做一个测验应用程序。我正在从一个API获取数据,并将其显示在活动中。现在我正在实现一个进度条,它指示回答每个问题的时间。这是我从API获取的函数:

 private fun fetch(){

    val theme = intent.getIntExtra("sport", 21)
    val difficulty = intent.getStringExtra("medium")

    val url = "https://opentdb.com/api.php?amount=10&category=21&difficulty=medium&type=multiple"
    val request = Request.Builder()
        .url(url)
        .build()

    client.newCall(request).enqueue(object: Callback {

        override fun onResponse(call: Call, response: Response) {

            val body = response.body?.string()
            val gson = GsonBuilder().create()
            val triviaRequest = gson.fromJson(body, TriviaRequest::class.java)
            val result = triviaRequest.results[0]
            val question = result.question.toSpanned()
            val questionCategory = result.category.toSpanned()
            val correctAnswer = result.correct_answer.toSpanned()
            val incorrectAnswer1 = result.incorrect_answers[0].toSpanned()
            val incorrectAnswer2 = result.incorrect_answers[1].toSpanned()
            val incorrectAnswer3 = result.incorrect_answers[2].toSpanned()

            val questions = listOf(
                correctAnswer,
                incorrectAnswer1,
                incorrectAnswer2,
                incorrectAnswer3
            )

            shuffle(questions)

            runOnUiThread {

                val button1 = findViewById<Button>(R.id.alternative1)
                val button2 = findViewById<Button>(R.id.alternative2)
                val button3 = findViewById<Button>(R.id.alternative3)
                val button4 = findViewById<Button>(R.id.alternative4)

                questionText.text = question
                themeText.text = questionCategory

                button1.text = questions[0]
                button1.isEnabled = true

                button2.text = questions[1]
                button2.isEnabled = true

                button3.text = questions[2]
                button3.isEnabled = true

                button4.text = questions[3]
                button4.isEnabled = true

                button1.setOnClickListener {

                    button1.isClickable = false
                    if (button1.text == correctAnswer) {
                        score += 1
                        fetch()
                    } else {
                        button1.text = "---"
                    }
                }

                button2.setOnClickListener {

                    button2.isClickable = false
                    if (button2.text == correctAnswer) {
                        score += 1
                        fetch()
                    } else {
                        button2.text = "---"
                    }
                }

                button3.setOnClickListener {

                    button3.isClickable = false
                    if (button3.text == correctAnswer) {
                        score += 1
                        fetch()
                    } else {
                        button3.text = "---"
                    }
                }

                button4.setOnClickListener {

                    button4.isClickable = false
                    if (button4.text == correctAnswer) {
                        score += 1
                        fetch()
                    } else {
                        button4.text = "---"
                    }
                }
            }
            response.close()
        }

        override fun onFailure(call: Call, e: IOException) {
            e.printStackTrace()
        }

    })
}
private fun fetch(){
val theme=intent.getIntExtra(“运动”,21)
val难度=intent.getStringExtra(“中等”)
val url=”https://opentdb.com/api.php?amount=10&category=21&difficulty=medium&type=multiple"
val request=request.Builder()
.url(url)
.build()
client.newCall(请求).enqueue(对象:回调{
覆盖fun onResponse(调用:调用,响应:响应){
val body=response.body?.string()
val gson=GsonBuilder().create()
val triviaRequest=gson.fromJson(正文,triviaRequest::class.java)
val结果=繁琐的请求。结果[0]
val question=result.question.tospan()
val questionCategory=result.category.tospan()
val correctAnswer=result.correct_answer.tospan()
val incorrectAnswer1=结果。回答不正确[0]。toSpanned()
val incorrectAnswer2=结果。回答不正确[1]。toSpanned()
val incorrectAnswer3=结果。回答不正确[2]。toSpanned()
val问题=列表(
回答正确,
不正确的答案1,
不正确的答案2,
不正确的答案3
)
洗牌(问题)
runOnUiThread{
val button1=findViewById(R.id.alternative1)
val按钮2=findViewById(R.id.alternative2)
val按钮3=findViewById(R.id.alternative3)
val按钮4=findViewById(R.id.alternative4)
questionText.text=问题
themeText.text=问题类别
button1.text=问题[0]
按钮1.isEnabled=真
按钮2.text=问题[1]
按钮2.isEnabled=真
button3.text=问题[2]
button3.isEnabled=true
按钮4.text=问题[3]
按钮4.isEnabled=真
button1.setOnClickListener{
button1.isClickable=false
如果(button1.text==correctAnswer){
分数+=1
fetch()
}否则{
button1.text=“--”
}
}
button2.setOnClickListener{
按钮2.isClickable=false
if(button2.text==correctAnswer){
分数+=1
fetch()
}否则{
button2.text=“--”
}
}
button3.setOnClickListener{
button3.isClickable=false
if(button3.text==correctAnswer){
分数+=1
fetch()
}否则{
button3.text=“--”
}
}
按钮4.setOnClickListener{
按钮4.isClickable=false
如果(按钮4.text==correctAnswer){
分数+=1
fetch()
}否则{
button4.text=“--”
}
}
}
答复:close()
}
覆盖失效(调用:调用,e:IOException){
e、 printStackTrace()
}
})
}
下面是我显示进度条的方式:

  val fetch = runCatching { fetch() }
        if(fetch.isSuccess) {
            val handler = Handler()
            Thread(Runnable {

                while (progressCount < 100) {
                    progressCount += 1
                    try {
                        Thread.sleep(50)
                    } catch (e: InterruptedException) {
                        e.printStackTrace()
                    }
                    handler.post {
                        progressBar.progress = progressCount
                    }
                }
            }).start()
        }

    }
val fetch=runCatching{fetch()}
if(fetch.issucess){
val handler=handler()
线程(可运行{
而(进程计数<100){
progressCount+=1
试一试{
线程。睡眠(50)
}捕获(e:中断异常){
e、 printStackTrace()
}
handler.post{
progressBar.progress=progressCount
}
}
}).start()
}
}
现在的问题是,活动一开始,我的进度条就开始显示,但问题还没有出现。我的问题是,如何知道我的fetch函数何时完成执行? 这样我就可以显示进度条了


谢谢

您可以查看kotlin协程并使用挂起函数,也可以使用如下回调:

fun fetch (callback: ()->Unit) { // take callback param

   ... // do the stuff

   callback() // call the callback on complete

}

fetch {
    // Done fetching here, execute your code inside this
}