Skip to content
This repository has been archived by the owner on Jul 23, 2022. It is now read-only.

Annotation to avoid a property being filled in ContentValues #25

Closed
dianaghele opened this issue Apr 7, 2016 · 4 comments · Fixed by #28
Closed

Annotation to avoid a property being filled in ContentValues #25

dianaghele opened this issue Apr 7, 2016 · 4 comments · Fixed by #28

Comments

@dianaghele
Copy link

Is there any annotation or something to add to a property in order for it to be left out when building toContentValues?
I would need this for the Ids that are autoincremented primary keys.

Thanks!

@gabrielittner
Copy link
Owner

Currently there isn't a way, but you can add and implement a second toValues() method that calls the abstract one and then removes the id or other columns.

@AutoValue public abstract class User {
  abstract String id();
  abstract String name();
  abstract String email();

  // you can choose whichever name you want for the method
  abstract ContentValues toCompleteContentValues();

  public ContentValues toContentValues() {
    return toCompleteContentValues().remove("id");
  }
}

@dianaghele
Copy link
Author

Thanks for the info!

Do you plan to add something to resolve this?

@gabrielittner
Copy link
Owner

You can use the new @ValuesAdapter annotation for custom types in 0.4.0-SNAPSHOT to ignore fields when creating ContentValues.

public final class IgnoreFieldAdapter {
  public static ContentValues ignoreField(String s) {
    return null;
  }
}
@AutoValue public abstract class User {
  @ValuesAdapter(IgnoreFieldAdapter.class) abstract String id();
  abstract String name();
  abstract String email();

  abstract ContentValues toValues();
}

@DanielHeckrath
Copy link

I'm wondering since ValuesAdapter is no longer a thing, how is this supposed to work with a ColumnTypeAdapter?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants