简体   繁体   English

如何从 getIntent 为 EditActivity 的视图模型设置第一个值

[英]How do I set first values to the viewmodel of EditActivity from getIntent

I have a recyclerview containing rows of info about fruits like name, color, weight, description, matching food...我有一个 recyclerview 包含有关水果的信息行,如名称、颜色、重量、描述、匹配的食物......

if i click a button it should load those row values into the intent and take me to startActivityForResult: EditActivity.如果我单击一个按钮,它应该将这些行值加载到 Intent 中,并将我带到 startActivityForResult: EditActivity。

Well the reason for the viewmodel is to prevent rotation from resetting the values back to the ones from getIntent.好吧,视图模型的原因是防止旋转将值重置回 getIntent 中的值。 The values only get updated to sql in the mainactivity onreturnresultsomething... method.值仅在 mainactivity onreturnresultsomething... 方法中更新为 sql。

if i do '''mviewmodel.name = intent.getStringExtra(name,""):''' in mainactivity oncreate, that would just overrite it on rotation just as if there was no viewmodel at all.如果我在 mainactivity oncreate 中执行 '''mviewmodel.name = intent.getStringExtra(name,""):''',那只会在旋转时覆盖它,就好像根本没有视图模型一样。

I'm thinking of using sharedpreferences, but there's a better way, and androidviewmodel is an antipattern.我正在考虑使用共享首选项,但有更好的方法,而 androidviewmodel 是一种反模式。

so from getintent how do i add default values to viewmodel in editactivity and that it doesnt get set back to that on rotation?因此,从 getintent 开始,我如何将默认值添加到编辑活动中的视图模型,并且它不会在旋转时设置回原来的值?

thanks all!谢谢大家!

put your values on [SavedStateHandle]: https://developer.android.com/reference/androidx/lifecycle/SavedStateHandle将您的值放在 [SavedStateHandle] 上: https : //developer.android.com/reference/androidx/lifecycle/SavedStateHandle

mviewmodel.savedStateHandle.set("name",name)

before you set the value, check if it is null or not, something like:在设置值之前,请检查它是否为空,例如:

if(mviewmodel.savedStateHandle.get("name) == null){
   mviewmodel.savedStateHandle.set("name",name)
}

you can check it from here: https://developer.android.com/topic/libraries/architecture/viewmodel-savedstate您可以从这里查看: https : //developer.android.com/topic/libraries/architecture/viewmodel-savedstate

If you are using the current by viewModels or the underlying ViewModelProvider pattern your ViewModel will not be recreated, so you could add it as a constructor parameter.如果您使用当前的by viewModels或底层的ViewModelProvider模式,您的 ViewModel 将不会被重新创建,因此您可以将其添加为构造函数参数。

Check out ViewModelProvider.Factory for how to do that.查看ViewModelProvider.Factory了解如何做到这一点。 Additionally if you plan on using Dagger, but you still have parameters, that you'd like to pass in manually you can check out this library: https://github.com/halcyonmobile/ViewModelFactoryGenerator此外,如果您打算使用 Dagger,但仍有参数,您想手动传入,您可以查看此库: https : //github.com/halcyonmobile/ViewModelFactoryGenerator

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 getIntent().getSerializableExtra 从数据库中检索数据 - How do i use getIntent().getSerializableExtra to retrieve data from the database 如何使用getIntent()更新主要活动? - How do I update the Main Activity using getIntent()? 如何在 ViewModel 中观察 FutureTask 的结果? - How do I observe a result from FutureTask in ViewModel? "(Integer)getIntent().getExtras().get(x)" 与 "(Integer)getIntent().getStringExtra(x)" 有何不同? - How is "(Integer)getIntent().getExtras().get(x)" different from "(Integer)getIntent().getStringExtra(x)"? 如何在使用Intent集调用Activity后使getIntent()返回null - How to get getIntent() to return null after Activity called with an Intent set 如何从CS50的第一个问题集中获得用户的输入? - How do I get input from user in the first problem set of cs50? 如何从扩展AppCompatActivity访问getIntent()扩展应用程序 - How to access getIntent() from extends AppCompatActivity to extends Application java - 如何从java中的结果集中迭代行的值? - How do I iterate through the values of a row from a result set in java? 如何从 ViewModel 中获取列表以显示在我的微调器中并可选择? - How do I get a list from my ViewModel to display in my spinner and be selectable? 如何使用对象类中某些值的结果来计算其他值并在创建时进行设置? - How do I use the results from some values in an object class to calculate others and set when created?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM