There's a fragment on the app I'm working on that the user needs to fill up to 6 EditText
. Each pair of EditText
represents the type and description of the user's address (as in "Apartment" as the Type and "1201" as the description). According to Brazil's postal service company, it is possible to add up to 3 complements.
At default, I'm showing one pair of EditText
that represents the first complement. There's a button that let the user add two more address complements.
I want to use LiveData
and DataBinding
to update an array of Complements on my ViewModel but it isn't working and I have no idea why.
Is it possible to work with arrays and 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;
}
Fragment
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);