В моем файле ресурсов есть следующий EditText:
<data>
<variable name = "assessment" type = "Models.Assessment"/>
<variable name = "assessmentVM" type = "ViewModel.AssessmentViewModel"/>
</data>
<ScrollView
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:scrollbars = "vertical">
<LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical">
<TextView
style = "@style/AssessorTextView"
android:text = "Date Inspected"/>
<EditText
style = "@style/AssessorEditText"
android:hint = "Date inspected"
android:text = "@ = {assessment.inspectedDate}"
android:focusable = "false"
android:editable = "false"
android:clickable = "true"
android:onClick = "@{assessmentVM::onCalendarSelection}"/>
</LinearLayout>
</ScrollView>
Вот мой OnDateSetListener:
DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.YEAR, year);
SetDateOnModel(year, month, dayOfMonth);
}
};
И, наконец, мой метод SetDateOnModel:
public void SetDateOnModel(int year, int month, int day){
try{
StringBuilder builder = new StringBuilder();
builder.append(month).append("/").append(month).append("/").append(day);
this.assessment.setInspectedDate(builder.toString());
ab.executePendingBindings();
} catch (Exception exception){
assessment.setInspectedDate(new Date().toString());
}
}
При выборе даты в моем DatePickerDialog я хотел бы, чтобы выбранная дата отображалась как String в моем EditText, но в настоящее время этого не происходит. Однако я вижу InspectedDate в переменной оценки, когда нажимаю кнопку «Отправить» и отлаживаю экземпляр экзамена.
Кажется, что моя двусторонняя привязка данных не работает, когда я устанавливаю установщик setInspectedDate() экземпляра Assessment.
Как видно в модели Assessment, она расширяет BaseObservable, а свойства - @Bindable.
public class Assessment extends BaseObservable implements Parcelable {
public Assessment(){
}
@SerializedName("InspectedDate")
private String inspectedDate;
@Bindable
public String getInspectedDate() {
return inspectedDate;
}
public void setInspectedDate(String inspectedDate) {
this.inspectedDate = inspectedDate;
}
protected Assessment(Parcel in) {
inspectedDate = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(inspectedDate);
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<Assessment> CREATOR = new Parcelable.Creator<Assessment>() {
@Override
public Assessment createFromParcel(Parcel in) {
return new Assessment(in);
}
@Override
public Assessment[] newArray(int size) {
return new Assessment[size];
}
};
}




Убедитесь, что ваша модель Assessment расширяет android.databinding.BaseObservable, а методы получения и установки свойства связаны:
@Bindable
public String getInspectedDate() {
return this.inspectedDate;
}
public void setInspectedDate(String inspectedDate) {
this.inspectedDate = inspectedDate;
notifyPropertyChanged(BR.inspectedDate);
}
Обновил мой исходный пост
Я вижу свою проблему благодаря твоему коду. Я забыл добавить notifyPropertyChanged.
Он расширяет BaseObservable, и, к сожалению, свойства привязаны