如何在数据绑定中的多个视图之间共享LiveData变量?

时间:2019-12-09 12:02:19

标签: android android-databinding android-livedata android-viewmodel

我有两个字段和按钮的表格。当用户单击按钮时,我将隐藏所有视图的Errors(如果有的话),我将再次开始验证。以下是我的 Sub B_PDFs() Dim PDFarray As String, PDFName as String, sht As String Sheets("Control").Select PLFile = ActiveWorkbook.Name PDFLoc = Application.ActiveWorkbook.Path & "\" PDFName = Range("A20") PDFSheetCount = Range("J1").Offset(Rows.Count - 1, 0).End(xlUp).Row 'Loop through column J and create a string with each tab name to be exported For x = 2 To PDFSheetCount Step 1 If x = PDFSheetCount Then sht = """ " & "" & Cells(x, 10) & """ " Else sht = """" & "" & Cells(x, 10) & """" & ", " End If PDFarray = PDFarray & sht Next x 'Create PDF from the array above Worksheets(Array(PDFarray)).Select - this is where I get the error Subscript Out Of Range Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PFDLoc & PDFName, Quality:= _ xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False Workbooks(PLFile).Activate End Sub 按钮点击代码

viewmodel

_resetErrors.value = true if (_bankAccount.value?.accountNumber.isNullOrBlank()) { accountNumberError.value = "Please enter account number" fieldLevelError.value = accountNumberError.value return false } if (TextUtils.isEmpty(_bankAccount.value?.routNumber?.trim())) { routingNumberError.value = "Please enter rout number" fieldLevelError.value = routNumberError.value return false } 代码如下

XML

android:id="@+id/layout_add_manual_account_account_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/dimen_16dp" android:imeOptions="actionNext" android:inputType="number|textNoSuggestions" android:maxLines="1" android:text="@={bankAccountViewModel.bankAccount.accountNumber}" android:textAllCaps="true" app:errorText="@{bankAccountViewModel.accountNumberError}" app:resetError="@{bankAccountViewModel._resetErrors}" /> <com.transfast.transfast.v4.customviews.TFTextControlView android:id="@+id/layout_add_manual_account_routing_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/dimen_16dp" android:imeOptions="actionNext" android:inputType="number|textNoSuggestions" android:maxLines="1" android:text="@={bankAccountViewModel.bankAccount.routingNumber}" android:textAllCaps="true" app:errorText="@{bankAccountViewModel.routNumberError}" app:resetError="@{bankAccountViewModel._resetErrors}" /> 代码如下

Binding Adapter

由于我想隐藏所有字段的错误,因此我在两个字段之间共享@BindingAdapter("errorText") fun setError(tfTextControlView: TFTextControlView, errorText: String) { if(!errorText.isNullOrBlank()) { tfTextControlView.error = errorText } } @BindingAdapter("resetError") fun resetError(tfTextControlView: TFTextControlView, isReset: Boolean) { if (isReset) { tfTextControlView.hideError() } 。但是在绑定适配器中,_resetErrorssetError之前被调用,并且错误在错误场景中从未显示。如果有多个字段,重置错误的最佳方法是什么?(我有2个,但如果有20个,创建20个错误变量是不可行的。)还是如何使用实时数据重置多个字段的错误?

}

0 个答案:

没有答案