JetPack组成ViewModel无法在方法上获得价值
我有一个具有私有函数的类,以及私有的方法,当我从普通类方法访问视图模型时,我无法从我的视图模型中获得值,但是在合并的内部,我可以访问它我该怎么办?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用了与您的ViewModel中相同的代码,但是我修复了编译器错误的
livedata&gt;
livedata&lt; string&gt;
。对于此示例,我还将空的启动状态字符串更改为“初始值”
。在您的
示例
类代码中,我修复了另一个编译器警告,其中您调用的函数名称与实际函数名称不匹配。因此,我将state.getSomething()
更改为state.getSomeValue()
,然后添加了log.d.d
调用来记录结果。如果您还想返回结果(值),请添加返回语句,然后将返回类型更改为
fun for get getsomeValue()
的字符串。然后,我制作了一个可组合功能/组件,在其中调用您的同类功能。
我在日志中获得了预期的结果:
I used the same code as in your ViewModel, but I fixed the compiler error for
LiveData<Int>
toLiveData<String>
. I also changed the empty starting state string to"initial value"
for this example.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 changedstate.getSomething()
tostate.getSomeValue()
and I added aLog.d
call to log the 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.
And I get the expected results in the log: