JetPack组成ViewModel无法在方法上获得价值

发布于 2025-02-08 05:07:20 字数 1085 浏览 3 评论 0原文

我有一个具有私有函数的类,以及私有的方法,当我从普通类方法访问视图模型时,我无法从我的视图模型中获得值,但是在合并的内部,我可以访问它我该怎么办?

class Example(){
    private var state: myviewmodel = myviewmodel()

    @Composable
    private fun mycomposable(){
      state.updateSomething()
    }

    fun getSomeValue(){
      //not working
      state.getSomething()
    }
}

这是我尝试的东西,

class Example(){
    private var state: myviewmodel = myviewmodel()
    private var holdSomething:String = ""
    
    @Composable
    private fun mycomposable(){
      state.updateSomething()
      holdSomething = state.getSomething()
    }

    fun getSomeValue(){
      //not working
      holdSomething
      }
}

我在这里做错了什么?

这里更新

是我的ViewModel

class myviewmodel: ViewModel(){
    private val _something: MutableLiveData<String> = MutableLiveData("")
    val something: LiveData<Int> = _something

    fun updateSomething(){
      _something.value = "newvalue"
    }

    fun getSomeValue(): String {
      return something.value? : ""
    }
}

I have a class that has composable functions which is private as well as methods i cant get a value from my view model when accessing it from a normal class method but inside a composable i can access it what can i do?

class Example(){
    private var state: myviewmodel = myviewmodel()

    @Composable
    private fun mycomposable(){
      state.updateSomething()
    }

    fun getSomeValue(){
      //not working
      state.getSomething()
    }
}

here is what i tried

class Example(){
    private var state: myviewmodel = myviewmodel()
    private var holdSomething:String = ""
    
    @Composable
    private fun mycomposable(){
      state.updateSomething()
      holdSomething = state.getSomething()
    }

    fun getSomeValue(){
      //not working
      holdSomething
      }
}

What am i doing Wrong here?

Updated

Here is my viewmodel

class myviewmodel: ViewModel(){
    private val _something: MutableLiveData<String> = MutableLiveData("")
    val something: LiveData<Int> = _something

    fun updateSomething(){
      _something.value = "newvalue"
    }

    fun getSomeValue(): String {
      return something.value? : ""
    }
}

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

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

发布评论

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

评论(1

独夜无伴 2025-02-15 05:07:20

我使用了与您的ViewModel中相同的代码,但是我修复了编译器错误的livedata&gt; livedata&lt; string&gt;。对于此示例,我还将空的启动状态字符串更改为“初始值”

class myviewmodel: ViewModel(){
    private val _something: MutableLiveData<String> = MutableLiveData("initial value")
    val something: LiveData<String> = _something

    fun updateSomething() {
        _something.value = "new value"
    }

    fun getSomeValue(): String {
        return something.value ?: ""
    }
}

在您的示例类代码中,我修复了另一个编译器警告,其中您调用的函数名称与实际函数名称不匹配。因此,我将state.getSomething()更改为state.getSomeValue(),然后添加了log.d.d调用来记录结果。

class Example {
    private var state: myviewmodel = myviewmodel()

    @Composable
    fun mycomposable() {
        state.updateSomething()
    }

    fun getSomeValue() {
        val result = state.getSomeValue()
        Log.d("Test", result)
    }
}

如果您还想返回结果(值),请添加返回语句,然后将返回类型更改为fun for get getsomeValue()的字符串。

然后,我制作了一个可组合功能/组件,在其中调用您的同类功能。

@Composable
fun MyComponent() {
    val example = Example()

    // this logs "initial value"
    example.getSomeValue()

    example.mycomposable()

    // this logs "new value"
    example.getSomeValue()
}

我在日志中获得了预期的结果:

D/Test: initial value
D/Test: new value

I used the same code as in your ViewModel, but I fixed the compiler error for LiveData<Int> to LiveData<String>. I also changed the empty starting state string to "initial value" for this example.

class myviewmodel: ViewModel(){
    private val _something: MutableLiveData<String> = MutableLiveData("initial value")
    val something: LiveData<String> = _something

    fun updateSomething() {
        _something.value = "new value"
    }

    fun getSomeValue(): String {
        return something.value ?: ""
    }
}

In your Example class code I fixed another compiler warning where the name of the function you call does not match the actual function name. So I changed state.getSomething() to state.getSomeValue() and I added a Log.d call to log the result.

class Example {
    private var state: myviewmodel = myviewmodel()

    @Composable
    fun mycomposable() {
        state.updateSomething()
    }

    fun getSomeValue() {
        val result = state.getSomeValue()
        Log.d("Test", result)
    }
}

If you also want to return the result (the value) then add a return statement and change the return type to String for fun getSomeValue().

Then I made a Composable function/component where I call both functions from your class.

@Composable
fun MyComponent() {
    val example = Example()

    // this logs "initial value"
    example.getSomeValue()

    example.mycomposable()

    // this logs "new value"
    example.getSomeValue()
}

And I get the expected results in the log:

D/Test: initial value
D/Test: new value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文