可以 Android 数据绑定 getters return 特定字段动态地基于 getter 的参数吗?
Can Android databinding getters return specific fields dynamically based on the getter's argument?
编译器抛出构建异常@Bindable associated with method must follow JavaBeans convention related to a getter that's marked @Bindable, which accepts one parameter (phototag) 和 returns 基于该参数值的特定字段值:
private boolean operationPhotoSaved;
private boolean material1PhotoSaved;
private boolean material2PhotoSaved;
private boolean material3PhotoSaved;
private boolean material4PhotoSaved;
private boolean material5PhotoSaved;
@Bindable
public boolean isPhotoSaved (PhotoTag photoTag){
boolean isSaved = false;
switch (photoTag) {
case OPERATIONPHOTO:
isSaved = this.operationPhotoSaved;
break;
case MATERIAL1PHOTO:
isSaved = this.material1PhotoSaved;
break;
case MATERIAL2PHOTO:
isSaved = this.material2PhotoSaved;
break;
case MATERIAL3PHOTO:
isSaved = this.material3PhotoSaved;
break;
case MATERIAL4PHOTO:
isSaved = this.material4PhotoSaved;
break;
case MATERIAL5PHOTO:
isSaved = this.material5PhotoSaved;
break;
}
return isSaved;
}
这似乎在 Android 数据绑定中是不允许的?
我最终通过为每个字段创建标准的 getter 和 setter 并将 @Bindable 标记设置为 getter 而避免了错误,同时将其从 isPhotoSaved 方法中删除,然后调用 getter从该方法中:
private boolean operationPhotoSaved;
private boolean material1PhotoSaved;
private boolean material2PhotoSaved;
private boolean material3PhotoSaved;
private boolean material4PhotoSaved;
private boolean material5PhotoSaved;
private boolean videoSaved;
public boolean isPhotoSaved(PhotoTag photoTag) {
boolean isSaved = false;
switch (photoTag) {
case OPERATIONPHOTO:
isSaved = isOperationPhotoSaved();
break;
case MATERIAL1PHOTO:
isSaved = isMaterial1PhotoSaved();
break;
case MATERIAL2PHOTO:
isSaved = isMaterial2PhotoSaved();
break;
case MATERIAL3PHOTO:
isSaved = isMaterial3PhotoSaved();
break;
case MATERIAL4PHOTO:
isSaved = isMaterial4PhotoSaved();
break;
case MATERIAL5PHOTO:
isSaved = isMaterial5PhotoSaved();
break;
}
return isSaved;
}
@Bindable
public boolean isOperationPhotoSaved() {
return operationPhotoSaved;
}
public void setOperationPhotoSaved(boolean operationPhotoSaved) {
this.operationPhotoSaved = operationPhotoSaved;
}
@Bindable
public boolean isMaterial1PhotoSaved() {
return material1PhotoSaved;
}
public void setMaterial1PhotoSaved(boolean material1PhotoSaved) {
this.material1PhotoSaved = material1PhotoSaved;
}
@Bindable
public boolean isMaterial2PhotoSaved() {
return material2PhotoSaved;
}
public void setMaterial2PhotoSaved(boolean material2PhotoSaved) {
this.material2PhotoSaved = material2PhotoSaved;
}
@Bindable
public boolean isMaterial3PhotoSaved() {
return material3PhotoSaved;
}
public void setMaterial3PhotoSaved(boolean material3PhotoSaved) {
this.material3PhotoSaved = material3PhotoSaved;
}
@Bindable
public boolean isMaterial4PhotoSaved() {
return material4PhotoSaved;
}
public void setMaterial4PhotoSaved(boolean material4PhotoSaved) {
this.material4PhotoSaved = material4PhotoSaved;
}
@Bindable
public boolean isMaterial5PhotoSaved() {
return material5PhotoSaved;
}
public void setMaterial5PhotoSaved(boolean material5PhotoSaved) {
this.material5PhotoSaved = material5PhotoSaved;
}
编译器抛出构建异常@Bindable associated with method must follow JavaBeans convention related to a getter that's marked @Bindable, which accepts one parameter (phototag) 和 returns 基于该参数值的特定字段值:
private boolean operationPhotoSaved;
private boolean material1PhotoSaved;
private boolean material2PhotoSaved;
private boolean material3PhotoSaved;
private boolean material4PhotoSaved;
private boolean material5PhotoSaved;
@Bindable
public boolean isPhotoSaved (PhotoTag photoTag){
boolean isSaved = false;
switch (photoTag) {
case OPERATIONPHOTO:
isSaved = this.operationPhotoSaved;
break;
case MATERIAL1PHOTO:
isSaved = this.material1PhotoSaved;
break;
case MATERIAL2PHOTO:
isSaved = this.material2PhotoSaved;
break;
case MATERIAL3PHOTO:
isSaved = this.material3PhotoSaved;
break;
case MATERIAL4PHOTO:
isSaved = this.material4PhotoSaved;
break;
case MATERIAL5PHOTO:
isSaved = this.material5PhotoSaved;
break;
}
return isSaved;
}
这似乎在 Android 数据绑定中是不允许的?
我最终通过为每个字段创建标准的 getter 和 setter 并将 @Bindable 标记设置为 getter 而避免了错误,同时将其从 isPhotoSaved 方法中删除,然后调用 getter从该方法中:
private boolean operationPhotoSaved;
private boolean material1PhotoSaved;
private boolean material2PhotoSaved;
private boolean material3PhotoSaved;
private boolean material4PhotoSaved;
private boolean material5PhotoSaved;
private boolean videoSaved;
public boolean isPhotoSaved(PhotoTag photoTag) {
boolean isSaved = false;
switch (photoTag) {
case OPERATIONPHOTO:
isSaved = isOperationPhotoSaved();
break;
case MATERIAL1PHOTO:
isSaved = isMaterial1PhotoSaved();
break;
case MATERIAL2PHOTO:
isSaved = isMaterial2PhotoSaved();
break;
case MATERIAL3PHOTO:
isSaved = isMaterial3PhotoSaved();
break;
case MATERIAL4PHOTO:
isSaved = isMaterial4PhotoSaved();
break;
case MATERIAL5PHOTO:
isSaved = isMaterial5PhotoSaved();
break;
}
return isSaved;
}
@Bindable
public boolean isOperationPhotoSaved() {
return operationPhotoSaved;
}
public void setOperationPhotoSaved(boolean operationPhotoSaved) {
this.operationPhotoSaved = operationPhotoSaved;
}
@Bindable
public boolean isMaterial1PhotoSaved() {
return material1PhotoSaved;
}
public void setMaterial1PhotoSaved(boolean material1PhotoSaved) {
this.material1PhotoSaved = material1PhotoSaved;
}
@Bindable
public boolean isMaterial2PhotoSaved() {
return material2PhotoSaved;
}
public void setMaterial2PhotoSaved(boolean material2PhotoSaved) {
this.material2PhotoSaved = material2PhotoSaved;
}
@Bindable
public boolean isMaterial3PhotoSaved() {
return material3PhotoSaved;
}
public void setMaterial3PhotoSaved(boolean material3PhotoSaved) {
this.material3PhotoSaved = material3PhotoSaved;
}
@Bindable
public boolean isMaterial4PhotoSaved() {
return material4PhotoSaved;
}
public void setMaterial4PhotoSaved(boolean material4PhotoSaved) {
this.material4PhotoSaved = material4PhotoSaved;
}
@Bindable
public boolean isMaterial5PhotoSaved() {
return material5PhotoSaved;
}
public void setMaterial5PhotoSaved(boolean material5PhotoSaved) {
this.material5PhotoSaved = material5PhotoSaved;
}