简体   繁体   English

使用Android架构组件处理输入

[英]Handling input with Android Architecture Components

TL;DR TL; DR

How do I deal with Activities that actively change data (for example through an EditText)? 如何处理主动更改数据的活动(例如通过EditText)? Do I keep saving their state in the SavedInstanceState on rotation and only use the ViewModel when all of the fields are ready, or is there a way to make the ViewModel responsible for checking/holding/using the UI's data? 我是否继续在旋转时将状态保存在SavedInstanceState中,并且只在所有字段都准备就绪时使用ViewModel,或者是否有办法让ViewModel负责检查/保持/使用UI的数据?

Question

I'm developing my application using Google's Arch. 我正在使用Google的Arch开发我的应用程序。 Components, and writing my latest class I've noticed I'm not really sure on what the best practice is when handling, say, data coming from an Activity form. 组件和编写我的最新课程我注意到,在处理来自活动表单的数据时,我不确定最佳实践是什么。

Example

I have a POJO made of title, description, location, type 我有一个由标题,描述,位置,类型组成的POJO

I have an Activity with four EditText: title_et , description_et , location_et , type_et . 我有一个带有四个EditText的Activity: title_etdescription_etlocation_ettype_et

My ViewModel, through a Repository (irrelevant here), can send an object to the Database when the sendObject function is called. 我的ViewModel,通过一个Repository(这里不相关),可以在sendObject函数时将对象发送到数据库。

How I'm doing it now 我现在怎么做

The activity has the mTitle , mDescription , mLocation , mType . 该活动具有mTitlemDescriptionmLocationmType On rotation, the activity saves all of the EditText values in the savedInstanceState bundle, and it loads them again populating the views. 在旋转时,活动会将savedInstanceState包中的所有EditText值保存,然后再次加载它们以填充视图。

When the user wants to send the object, it clicks a button and the activity calls the function viewModel.sendObject(mTitle, mDescription, mLocation, mType) after the necessary checks. 当用户想要发送对象时,它会单击一个按钮,并且活动会在必要的检查后调用函数viewModel.sendObject(mTitle, mDescription, mLocation, mType)

Problems with this approach 这种方法存在问题

The activity is responsible of holding/checking all the data of the EditTexts, basically making the ViewModel only responsible of interacting with the Repository. 该活动负责保存/检查EditTexts的所有数据,基本上使ViewModel只负责与Repository的交互。

What I'd like to accomplish 我想要完成什么

Ideally, I'd want to make the Activity only responsible of the UI, delegating everything to the ViewModel. 理想情况下,我想让Activity只负责UI,将所有内容委托给ViewModel。

This way I could call sendObject() and the ViewModel would have already all of the data needed. 这样我可以调用sendObject() ,ViewModel就已经拥有了所有需要的数据。

The LiveData situation LiveData情况

Right now the ViewModel has only one instance of LiveData, inside that there is a Resource (which is taken from here ) and it's used to "tell" the Activity that new data has arrived or an error occurred. 现在,ViewModel只有一个LiveData实例,里面有一个Resource( 从这里取出 ),它用于“告诉”Activity新数据已经到达或发生了错误。

This approach works fine in all Activities that just receive data from the network and display them. 这种方法适用于仅从网络接收数据并显示它们的所有活动。 What do I do when I want to synchronise data coming FROM the Activity? 当我想同步来自活动的数据时,我该怎么办? Do I use one LiveData for each field and use that to display potential errors? 我是否为每个字段使用一个LiveData并使用它来显示潜在的错误?

I've read most of the samples but all of the Activities there are passive. 我已经阅读了大部分样本,但所有的活动都是被动的。

Conclusion 结论

Thanks to anyone who takes the time to help. 感谢任何花时间提供帮助的人。

您可以将逻辑分离为模型字符串类,另一个包含编辑文本字段的所有字符串值的类只是在类的顶部分配字符串值。

You can have a LiveData of your model in the ViewModel and alter it from the View (Activity/UI). 您可以在ViewModel中拥有模型的LiveData,并从View(Activity / UI)中更改它。 The downside is that to update the LiveData, you need to copy whole Model, edit it and post it back to live data. 缺点是要更新LiveData,您需要复制整个模型,编辑它并将其发布回实时数据。

The second way is to dissect Model's components in the ViewModel into individual parameter LiveDatas. 第二种方法是将ViewModel中的Model组件分解为单个参数LiveDatas。 Later when form is submitted you can reconstruct the Model. 稍后在提交表单时,您可以重建模型。

What you can do for native fields is use data binding. 您可以为本机字段执行的操作是使用数据绑定。 For other you need manually update LiveData from the View with listeners etc. 对于其他人,您需要使用侦听器等手动更新View中的LiveData。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM