我正在研究如何更新一些现有代码以使用新的 Android LiveData 架构模式。希望代码示例是不言自明的,我正在努力让@IntDef / @Interface 与实时数据一起工作。我很高兴在 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;
}