如何传递值

问题描述 投票:1回答:1

如何从此代码获取值time

val TAG = MainActivity::class.java.name
TrueTimeRx.build()

    .initializeRx("time.google.com")

    .subscribeOn(Schedulers.io())

    .subscribe({ time ->
        Log.v(TAG, "TrueTime was initialized and we have a time: $time") },
        { throwable -> throwable.printStackTrace() }

    )

并放在此代码中

helloWorld=findViewById(R.id.helloWorld)

val newTime=getString(R.string.hello, time)
helloWorld.text=newTime

如何从代码的第一部分获取time,然后将其放入第二部分

上面的代码在onCreate()

如果很重要,我有这个

internal lateinit var helloWorld: TextView
android kotlin rx-kotlin
1个回答
0
投票

该值将在订阅者块中提供:

TrueTimeRx.build()
    .initializeRx("time.google.com")
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe({ time ->
      Log.v(TAG, "TrueTime was initialized and we have a time: $time")
      val newTime = getString(R.string.hello, time)
      helloWorld.text = newTime
    }, { throwable -> throwable.printStackTrace() }
    )

注意,必须具有observeOn(AndroidSchedulers.mainThread())才能修改块内的View内容。

© www.soinside.com 2019 - 2024. All rights reserved.