我使用带有AutoValue
扩展名的auto-value-with
来创建略有改动的对象。
我的课程是:
@AutoValue
public abstract class FeedItem implements Parcelable {
public abstract String getAccountId();
public abstract String getName();
public abstract String getCompany();
@Nullable
public abstract String getAvatar();
@Nullable
public abstract String getPostImage();
@Nullable
public abstract String getThumb();
public abstract String getPostId();
public abstract String getFlaggedYes();
public abstract int getTotalFlags();
public abstract int getTotalComments();
public FeedItem withTotalFlags(int totalFlags) {
return builder().setTotalFlags(totalFlags).build();
}
public static Builder builder() {
return new AutoValue_FeedItem.Builder();
}
@AutoValue.Builder
public static abstract class Builder {
public abstract Builder setAccountId(String accountId);
public abstract Builder setName(String name);
public abstract Builder setCompany(String company);
public abstract Builder setAvatar(@Nullable String avatar);
public abstract Builder setPostImage(@Nullable String image);
public abstract Builder setThumb(@Nullable String thumb);
public abstract Builder setPostId(String postId);
public abstract Builder setFlaggedYes(String flaggedYes);
public abstract Builder setTotalFlags(int totalFlags);
public abstract Builder setTotalComments(int totalComments);
public abstract FeedItem build();
}
}
你可以看到我在哪里使用:
public FeedItem withTotalFlags(int totalFlags) {
return builder().setTotalFlags(totalFlags).build();
}
创建更改的对象。
问题在于,当我尝试使用它时:
FeedItem feedItem = dataset.get(position).withTotalFlags(updatedTotalFlags);
我明白了:
01-01 14:49:00.560 23369-23369/MY_PACKAGE E/AndroidRuntime: FATAL EXCEPTION: main
Process: MY_PACKAGE, PID: 23369
java.lang.IllegalStateException: Missing required properties: accountId name company postId flaggedYes totalComments
at MY_PACKAGE.data.models.$AutoValue_FeedItem$Builder.build($AutoValue_FeedItem.java:275)
at MY_PACKAGE.data.models.FeedItem.withTotalFlags(FeedItem.java:31)
at MY_PACKAGE.main.fragments.feed.adapter.FeedAdapter.updateItem(FeedAdapter.java:247)
at MY_PACKAGE.main.fragments.feed.FeedFragment.updateFlagState(FeedFragment.java:232)
at MY_PACKAGE.main.fragments.feed.FeedPresenter.onFlagStateRequestSuccess(FeedPresenter.java:97)
at MY_PACKAGE.main.fragments.feed.FeedRepository$1.onResponse(FeedRepository.java:72)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:1)
你不应该自己实施枯萎方法。只需将其声明为摘要,让auto-value-with
完成其工作。
public abstract FeedItem withTotalFlags(int totalFlags);
或者,您可以为模型声明toBuilder()
方法并使用它而不是创建新构建器。
abstract Builder toBuilder();
它将预填充原始对象中的所有值,并解决您的错误。
public FeedItem withTotalFlags(int totalFlags) {
return toBuilder().setTotalFlags(totalFlags).build();
}