0

编译器抛出与方法关联的构建异常@Bindable 必须遵循与标记为@Bindable 的getter 相关的JavaBeans 约定,该getter 接受一个参数(phototag)并根据该参数值返回特定字段的值:

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 数据绑定中是不允许的?

4

1 回答 1

0

我最终通过为每个字段创建标准 getter 和 setter 并在将其从isPhotoSaved方法中删除时将 @Bindable 标记设置为 getter 来避免错误,然后从该方法中调用 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;
}
于 2022-02-16T00:08:01.260 回答