我正在处理的应用程序上有一个片段,用户需要填充最多6个EditText
。每对EditText
代表用户地址的类型和描述(如“公寓”作为类型,“ 1201”作为描述)。根据巴西邮政服务公司的说法,最多可以添加3个补充。
默认情况下,我显示一对EditText
,它们代表第一个补码。有一个按钮可以让用户添加两个补充地址。
我想使用LiveData
和DataBinding
更新ViewModel上的补数数组,但它不起作用,我也不知道为什么。
是否可以使用数组和MutableLiveData
?
XML
<android.support.design.widget.TextInputLayout
android:id="@+id/tipo1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView">
<android.support.design.widget.TextInputEditText
android:id="@+id/tipo1edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Tipo 1"
android:text="@={viewModel.complementoList.getValues().get(0).descricao}" />
</android.support.design.widget.TextInputLayout>
ViewModel
private MutableLiveData<ArrayList<Complemento>> complementoList;
public MutableLiveData<ArrayList<Complemento>> getComplementoList() {
if (complementoList == null) {
complementoList = new MutableLiveData<>();
ArrayList<Complemento> arrayComplemento = new ArrayList<>();
arrayComplemento.add(new Complemento());
arrayComplemento.add(new Complemento());
arrayComplemento.add(new Complemento());
complementoList.setValue(arrayComplemento);
}
return complementoList;
}
片段
final Observer<ArrayList<Complemento>> complementoList = new Observer<ArrayList<Complemento>>() {
@Override
public void onChanged(@Nullable ArrayList<Complemento> complementos) {
//update stuff here
}
};
mViewModel.getComplementoList().observe(this, complementoList);