1

我正在处理的应用程序中有一个片段,用户需要填写 6 EditText。每对EditText代表用户地址的类型和描述(如“Apartment”为Type,“1201”为描述)。根据巴西邮政服务公司的说法,最多可以添加 3 个补码。

默认情况下,我显示一对EditText代表第一个补码。有一个按钮可以让用户再添加两个地址补码。

我想在我的 ViewModel 上使用LiveDataDataBinding更新一系列 Complements,但它不起作用,我不知道为什么。

是否可以使用数组和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>

视图模型

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);
4

0 回答 0