Search code examples
androidmodelsparcelableauto-value

How to update model value in android


I am using AutoValue in my models, I want to update the isTrue() value of the model when the user does something. So I need help. Here is my model.

@AutoValue
public abstract class Xyz implements Parcelable {

    @SerializedName("isTrue")
    public abstract boolean isTrue();

    @Nullable
    @SerializedName("lead_image_url")
    public abstract String lead_image_url();

    public static TypeAdapter<Readable> typeAdapter(Gson gson) {
        return new AutoValue_Readable.GsonTypeAdapter(gson);
    }
}

Solution

  • The use-case for @AutoValue is creating

    Generated immutable value classes ...

    If you want to change a value, you'd have to create a new instance of the type, updating this one value.

    This can be easily implemented with auto-value-with. Just add a with-method to your type.

    public abstract Xyz withIsTrue(boolean isTrue);
    

    The extension will implement the method copying all data to the new instance.