如何从mutableLiveData获取数据

发布于 2025-02-07 12:00:27 字数 692 浏览 2 评论 0原文

我正在尝试从mutableLivedata获取数据;但是,代码似乎有问题,你们能为我检查吗? 我可以得到对象,但是我未能将对象添加到mutablelist

properties = ArrayList()
    propertyViewModel.propertyItemLiveData.observe(
        viewLifecycleOwner,
        Observer { propertyItems ->

            for (property in propertyItems){
                var p:Property = Property(property.id,property.address
                ,property.price,property.phone,property.lat,property.lon)
                println(p)// i can display data

                properties.add(p)//when i add to properties, the properties still null. Why?
            }
        }
    )
    if (properties.isEmpty()){
        println("null")
    }

I'm trying to get data from a MutableLiveData; however, it seems like something is wrong with the code, can you guys check for me please?
I can get the object, but I failed to add the object to a mutableList

properties = ArrayList()
    propertyViewModel.propertyItemLiveData.observe(
        viewLifecycleOwner,
        Observer { propertyItems ->

            for (property in propertyItems){
                var p:Property = Property(property.id,property.address
                ,property.price,property.phone,property.lat,property.lon)
                println(p)// i can display data

                properties.add(p)//when i add to properties, the properties still null. Why?
            }
        }
    )
    if (properties.isEmpty()){
        println("null")
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水水月牙 2025-02-14 12:00:27

观察者中的代码只有在propertyEmlivedata按下新值时才能运行,或者当您首先观察 IT时它已经具有值。但是来自 docs

一旦应用程序组件在启动状态中,它就会从livedata对象接收最新值,它会观察到。仅当已设置要观察的livedata对象时,才会发生这种情况。

因此,直到您的活动fragment命中onstart()回调之前,您实际上不会获得值,这意味着您的观察者代码不会运行直到那时。如果您发布的代码要早(例如increate),那么您要做的是:

  • 创建一个空列表,
  • 添加一个观察者,该观察者会将内容添加到该列表中(但是它赢了' t运行直到稍后)
  • 检查列表是否仍然为空(肯定是),

因为观察者模式,您的代码对新数据/事件进行了反应 向其推开 ,无论您需要处理该填充的列表,都应是观察者代码的一部分。它应该对新值做出反应并采取行动 - 更新列表视图,提醒用户,启动API调用,

propertyViewModel.propertyItemLiveData.observe(viewLifecycleOwner) { propertyItems ->
    // handle the propertyItems, add them to your list etc

    // then do whatever needs to happen with the list, e.g. display it
    updateDisplay(propertyList)
}

如果propertydata class以及您'仅仅复制了所有数据,您就可以将其添加到您的列表中:

properties.addAll(propertyItems.map { it.copy() })
// or propertyItems.map(Property::copy)

The code in the observer will only run when propertyItemLiveData pushes a new value, or if it already has a value when you first observe it. But from the docs:

As soon as an app component is in the STARTED state, it receives the most recent value from the LiveData objects it’s observing. This only occurs if the LiveData object to be observed has been set.

So you won't actually get a value until your Activity or Fragment hits the onStart() callback, meaning your observer code won't run until then. If the code you posted is running earlier than that (say in onCreate), then what you're doing is:

  • creating an empty list
  • adding an observer that will add stuff to that list (but it won't run until later)
  • checking if the list is still empty (it definitely is)

Because of the observer pattern, where your code reacts to new data/events being pushed to it, whatever you need to do with that populated list should be part of the observer code. It should react to the new value and take action - update a list view, alert the user, start an API call, whatever

propertyViewModel.propertyItemLiveData.observe(viewLifecycleOwner) { propertyItems ->
    // handle the propertyItems, add them to your list etc

    // then do whatever needs to happen with the list, e.g. display it
    updateDisplay(propertyList)
}

btw if Property is a data class and you're just copying all its data, you can add to your list like this:

properties.addAll(propertyItems.map { it.copy() })
// or propertyItems.map(Property::copy)
场罚期间 2025-02-14 12:00:27

您好,首先是在Kotlin中,您必须使用MutableList,并且需要像这样的任何其他指令的检查。

properties =  mutableListOf<YourClass>()
    propertyViewModel.propertyItemLiveData.observe(
        viewLifecycleOwner,
        Observer { propertyItems ->

            for (property in propertyItems){
                var p:Property = Property(property.id,property.address
                ,property.price,property.phone,property.lat,property.lon)
                println(p)// i can display data

                properties.add(p)//when i add to properties, the properties 
            }

 if (properties.isEmpty()){
        println("null")
    }
        }
    )
   

hello first of all in kotlin in general you have to use mutableList and the check of empty or any other instruction should inside the call back like this :

properties =  mutableListOf<YourClass>()
    propertyViewModel.propertyItemLiveData.observe(
        viewLifecycleOwner,
        Observer { propertyItems ->

            for (property in propertyItems){
                var p:Property = Property(property.id,property.address
                ,property.price,property.phone,property.lat,property.lon)
                println(p)// i can display data

                properties.add(p)//when i add to properties, the properties 
            }

 if (properties.isEmpty()){
        println("null")
    }
        }
    )
   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文