我正在研究如何更新一些现有代码以使用新的Android LiveData架构模式。希望代码示例是自我解释的,我很难让@IntDef / @Interface与Live Data一起工作。我很高兴在ViewModel上为SetAnsweredCorrectly,SetCheated等创建方法,或者创建一个MutableLiveData属性并在代码中设置它,我只是在努力查看如何使用除正常Integer之外的任何东西,这会失去类型安全
public class QuestionViewModel extends ViewModel {
public static final int UNANSWERED = 0;
public static final int ANSWERED_CORRECTLY = 1;
public static final int ANSWERED_INCORRECTLY = 2;
public static final int CHEATED = 3;
@IntDef({UNANSWERED, ANSWERED_CORRECTLY, ANSWERED_INCORRECTLY, CHEATED})
@Retention(RetentionPolicy.SOURCE)
public @interface AnswerState{}
private @AnswerState int answeredState;
public Question()
{
this.setAnsweredState(UNANSWERED);
}
public @AnswerState int getAnsweredState() {
return answeredState;
}
public void setAnsweredState(@AnswerState int answeredState) {
this.answeredState = answeredState;
}
答案 0 :(得分:1)
尝试一下:
ObservableField<AnswerState> observableAnswer = new ObservableField<>;