Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.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 关于使用';liveData.observe';_Android_Kotlin_Android Livedata - Fatal编程技术网

Android 关于使用';liveData.observe';

Android 关于使用';liveData.observe';,android,kotlin,android-livedata,Android,Kotlin,Android Livedata,我正在自学Android开发。我想我已经了解了“liveData.observe”的工作原理。但是 制作一个简单的小应用程序,监听蓝牙设备的输入,并根据蓝牙输入显示或隐藏图像 我有所有的工作,但现在我想添加另一个功能,我遇到了一堵墙。该功能是每次liveData.Observe返回TRUE时增加一个计数器 要么我不理解“liveData.observe”,要么我试图错误地使用它(可能是这两种情况) 我正在做一个函数来增加这个计数器,这是我挂断电话的地方。我目前的想法是创建一个单独的函数(pigC

我正在自学Android开发。我想我已经了解了“liveData.observe”的工作原理。但是

制作一个简单的小应用程序,监听蓝牙设备的输入,并根据蓝牙输入显示或隐藏图像

我有所有的工作,但现在我想添加另一个功能,我遇到了一堵墙。该功能是每次liveData.Observe返回TRUE时增加一个计数器

要么我不理解“liveData.observe”,要么我试图错误地使用它(可能是这两种情况)

我正在做一个函数来增加这个计数器,这是我挂断电话的地方。我目前的想法是创建一个单独的函数(pigCounter())。但我什么也做不到

onCreate

class MainActivity : AppCompatActivity() {
private var liveData: MutableLiveData<String> = MutableLiveData()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
<SNIP>

...
</SNIP>

liveData.observe(this, androidx.lifecycle.Observer {
        imageView_mothership_LED_state.showOrHideImage(it == "1")
    })

pigCounter()

}
<ImageView
        android:id="@+id/imageView_mothership_LED_state"
        android:layout_width="124dp"
        android:layout_height="144dp"
        android:scaleType="fitCenter"
        android:soundEffectsEnabled="false"
        app:layout_constraintBottom_toBottomOf="@+id/imageView_blueYesOrNo"
        app:layout_constraintEnd_toEndOf="@+id/imageView_blueYesOrNo"
        app:layout_constraintStart_toStartOf="@+id/imageView_blueYesOrNo"
        app:layout_constraintTop_toTopOf="@+id/imageView_blueYesOrNo"
        app:srcCompat="@drawable/ic_check_black_24dp"
        android:contentDescription="Check mark image to indicate if LED is on/off" />
// display or don't display check mark image
private fun View.showOrHideImage(imageShow: Boolean) {
    visibility = if (imageShow) View.VISIBLE else View.GONE
}
这是处理传入蓝牙数据的功能

从母舰读取蓝牙数据()

这是我正在写的函数,我想每次增加一个计数器

猪数量

private fun pigCount() {
    var count = 0
    var counterTextView = findViewById<TextView>(R.id.textView_blueCounter)

    if(imageView_mothership_LED_state.showOrHideImage(true)) {
        counterTextView.text = count.toString()
        count++
    }
private fun pigCount(){
变量计数=0
var counterTextView=findviewbyd(R.id.textView\u blueCounter)
如果(imageView\u mothership\u LED\u state.showOrHideImage(真)){
counterTextView.text=count.toString()
计数++
}
由于类型不匹配(需要一个布尔值,获取单位),这不起作用。我还尝试将其移动到onCreate()中的liveData.observe函数中,但遇到了相同的障碍


有人能给我指出正确的方向吗?我有一种隐秘的感觉,我离这太远了。我会建议你轻推一下。:)

要计算一个
LiveData
返回
true
的次数真的很容易,你可以使用
中介LiveData

假设您有一个合适的
LiveData
,您可以执行以下操作:

val trueFalse = MutableLiveData<Boolean>()

val counter by lazy {
     MediatorLiveData<Int>().apply {
          value = 0 // Initialize the counter
          // Add the trueFalse as a source of this live data
          addSource(trueFalse) { boolVal ->
              if(boolVal == true) {
                  // If the mediator live data had a value, use it, if null use 0; and add +1 to it because the boolVal was true
                  value = (value ?: 0) + 1
              }
          }
     }
}

/* Somewhere else in the code */
fun setupTextView() {
    counter.observe({lifecycle}) {
        val tv = findViewById<TextView>(R.id.tv_something)
        tv.text = "$it"
    }
}
val trueFalse=MutableLiveData()
懒惰的瓦尔计数器{
MediatorLiveData().apply{
value=0//初始化计数器
//添加trueFalse作为此实时数据的源
addSource(trueFalse){boolVal->
如果(布尔值==真){
//如果中介活动数据有一个值,则使用该值;如果为null,则使用0;并向其添加+1,因为布尔值为true
值=(值?:0)+1
}
}
}
}
/*代码中的其他地方*/
有趣的设置文本视图(){
counter.observe({lifecycle}){
val tv=findviewbyd(R.id.tv\u某物)
tv.text=“$it”
}
}
这将创建一个
MediatorLiveData
,其中将包含trueFalse实时数据作为源。
如果值为
true
将继续并在
计数器中发布新值
实时数据。

感谢您的回复。我昨天和今天都在胡乱处理一些事情。但我当前现有的liveData实例不是布尔值,而是字符串。我可以看到应该发生什么。我只需要找出答案这两个位一起工作。如果我能让它工作,我会再次发布。谢谢你的帮助。然后只需将类型从布尔值交换为字符串,它应该像检查条件一样简单,应该将+1应用于计数器。是的,这就是我要处理的。再次感谢。将此标记为已回答。做得好,谢谢你的时间。
val trueFalse = MutableLiveData<Boolean>()

val counter by lazy {
     MediatorLiveData<Int>().apply {
          value = 0 // Initialize the counter
          // Add the trueFalse as a source of this live data
          addSource(trueFalse) { boolVal ->
              if(boolVal == true) {
                  // If the mediator live data had a value, use it, if null use 0; and add +1 to it because the boolVal was true
                  value = (value ?: 0) + 1
              }
          }
     }
}

/* Somewhere else in the code */
fun setupTextView() {
    counter.observe({lifecycle}) {
        val tv = findViewById<TextView>(R.id.tv_something)
        tv.text = "$it"
    }
}