在扩展 AbstractItem 的同时实现 RealmModel
Implement RealmModel while extending AbstractItem
我正在使用 FastAdapter
因为它很有趣并且让我从编写适配器 classes 中解脱出来。这是一个 class:
public class ProductsModel extends AbstractItem<ProductsModel, ProductsModel.ViewHolder> {
不过最近想用Realm
代替SQLite
。所以我想这样做:
public class ProductsModel extends AbstractItem<ProductsModel, ProductsModel.ViewHolder> implements RealmModel {
在我进行任何交易之前:
RealmResults<ProductsModel> productsModels = realm.where(ProductsModel.class).findAll(); // <- this line is generating the exception
if(productsModels.size() == 0){ //no product is saved, download now
}
但是得到这个异常:
java.lang.IllegalArgumentException: AbstractItem is not part of the schema for this Realm
请告诉我如何将 Realm 与 FastAdapter 一起使用。
如果您无法扩展 AbstractItem
,FastAdapter
允许您简单地实现 IItem
接口。您将必须实现您已经知道的功能 + 通常由 AbstractItem
实现的功能
这是一个示例项目:
public class SampleRealmItem extends RealmObject implements IItem<SampleRealmItem, SampleRealmItem.ViewHolder> {
// the identifier for this item
@PrimaryKey
protected long mIdentifier = -1;
@Override
public long getIdentifier() {
return mIdentifier;
}
// the tag for this item
@Ignore
protected Object mTag;
@Override
public Object getTag() {
return mTag;
}
// defines if this item is enabled
@Ignore
protected boolean mEnabled = true;
@Override
public boolean isEnabled() {
return mEnabled;
}
// defines if the item is selected
@Ignore
protected boolean mSelected = false;
@Override
public SampleRealmItem withSetSelected(boolean selected) {
this.mSelected = selected;
return this;
}
@Override
public boolean isSelected() {
return mSelected;
}
// defines if this item is selectable
@Ignore
protected boolean mSelectable = true;
@Override
public SampleRealmItem withSelectable(boolean selectable) {
this.mSelectable = selectable;
return this;
}
@Override
public boolean isSelectable() {
return mSelectable;
}
@Override
public int getType() {
return R.id.some_id;
}
@Override
public int getLayoutRes() {
return R.layout.some_layout;
}
@Override
public View generateView(Context ctx) {
ViewHolder viewHolder = getViewHolder(LayoutInflater.from(ctx).inflate(getLayoutRes(), null, false));
//as we already know the type of our ViewHolder cast it to our type
bindView(viewHolder, Collections.EMPTY_LIST);
//return the bound view
return viewHolder.itemView;
}
@Override
public View generateView(Context ctx, ViewGroup parent) {
ViewHolder viewHolder = getViewHolder(LayoutInflater.from(ctx).inflate(getLayoutRes(), parent, false));
//as we already know the type of our ViewHolder cast it to our type
bindView(viewHolder, Collections.EMPTY_LIST);
//return the bound and generatedView
return viewHolder.itemView;
}
@Override
public ViewHolder getViewHolder(ViewGroup parent) {
return getViewHolder(LayoutInflater.from(parent.getContext()).inflate(getLayoutRes(), parent, false));
}
private ViewHolder getViewHolder(View v) {
return new ViewHolder(v);
}
@Override
public void bindView(ViewHolder holder, List<Object> payloads) {
//set the selected state of this item. force this otherwise it may is missed when implementing an item
holder.itemView.setSelected(isSelected());
//set the tag of this item to this object (can be used when retrieving the view)
holder.itemView.setTag(this);
//handle your binding
}
@Override
public void unbindView(ViewHolder holder) {
holder.name.setText(null);
}
@Override
public void attachToWindow(ViewHolder holder) {}
@Override
public void detachFromWindow(ViewHolder holder) {}
@Override
public boolean failedToRecycle(ViewHolder holder) {
return false;
}
@Override
public boolean equals(int id) {
return id == mIdentifier;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AbstractItem<?, ?> that = (AbstractItem<?, ?>) o;
return mIdentifier == that.getIdentifier();
}
@Override
public int hashCode() {
return Long.valueOf(mIdentifier).hashCode();
}
protected static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View view) {
super(view);
}
}
}
为了简化它,我删除了注释和不必要的设置器。可以在此处的 FastAdapter
s 示例源中找到完整示例:
https://github.com/mikepenz/FastAdapter/blob/develop/app/src/main/java/com/mikepenz/fastadapter/app/items/RealmSampleUserItem.java
如果您有兴趣,可以在这里找到默认的 AbstractItem
实现:
https://github.com/mikepenz/FastAdapter/blob/develop/library-core/src/main/java/com/mikepenz/fastadapter/items/AbstractItem.java
我正在使用 FastAdapter
因为它很有趣并且让我从编写适配器 classes 中解脱出来。这是一个 class:
public class ProductsModel extends AbstractItem<ProductsModel, ProductsModel.ViewHolder> {
不过最近想用Realm
代替SQLite
。所以我想这样做:
public class ProductsModel extends AbstractItem<ProductsModel, ProductsModel.ViewHolder> implements RealmModel {
在我进行任何交易之前:
RealmResults<ProductsModel> productsModels = realm.where(ProductsModel.class).findAll(); // <- this line is generating the exception
if(productsModels.size() == 0){ //no product is saved, download now
}
但是得到这个异常:
java.lang.IllegalArgumentException: AbstractItem is not part of the schema for this Realm
请告诉我如何将 Realm 与 FastAdapter 一起使用。
如果您无法扩展 AbstractItem
,FastAdapter
允许您简单地实现 IItem
接口。您将必须实现您已经知道的功能 + 通常由 AbstractItem
这是一个示例项目:
public class SampleRealmItem extends RealmObject implements IItem<SampleRealmItem, SampleRealmItem.ViewHolder> {
// the identifier for this item
@PrimaryKey
protected long mIdentifier = -1;
@Override
public long getIdentifier() {
return mIdentifier;
}
// the tag for this item
@Ignore
protected Object mTag;
@Override
public Object getTag() {
return mTag;
}
// defines if this item is enabled
@Ignore
protected boolean mEnabled = true;
@Override
public boolean isEnabled() {
return mEnabled;
}
// defines if the item is selected
@Ignore
protected boolean mSelected = false;
@Override
public SampleRealmItem withSetSelected(boolean selected) {
this.mSelected = selected;
return this;
}
@Override
public boolean isSelected() {
return mSelected;
}
// defines if this item is selectable
@Ignore
protected boolean mSelectable = true;
@Override
public SampleRealmItem withSelectable(boolean selectable) {
this.mSelectable = selectable;
return this;
}
@Override
public boolean isSelectable() {
return mSelectable;
}
@Override
public int getType() {
return R.id.some_id;
}
@Override
public int getLayoutRes() {
return R.layout.some_layout;
}
@Override
public View generateView(Context ctx) {
ViewHolder viewHolder = getViewHolder(LayoutInflater.from(ctx).inflate(getLayoutRes(), null, false));
//as we already know the type of our ViewHolder cast it to our type
bindView(viewHolder, Collections.EMPTY_LIST);
//return the bound view
return viewHolder.itemView;
}
@Override
public View generateView(Context ctx, ViewGroup parent) {
ViewHolder viewHolder = getViewHolder(LayoutInflater.from(ctx).inflate(getLayoutRes(), parent, false));
//as we already know the type of our ViewHolder cast it to our type
bindView(viewHolder, Collections.EMPTY_LIST);
//return the bound and generatedView
return viewHolder.itemView;
}
@Override
public ViewHolder getViewHolder(ViewGroup parent) {
return getViewHolder(LayoutInflater.from(parent.getContext()).inflate(getLayoutRes(), parent, false));
}
private ViewHolder getViewHolder(View v) {
return new ViewHolder(v);
}
@Override
public void bindView(ViewHolder holder, List<Object> payloads) {
//set the selected state of this item. force this otherwise it may is missed when implementing an item
holder.itemView.setSelected(isSelected());
//set the tag of this item to this object (can be used when retrieving the view)
holder.itemView.setTag(this);
//handle your binding
}
@Override
public void unbindView(ViewHolder holder) {
holder.name.setText(null);
}
@Override
public void attachToWindow(ViewHolder holder) {}
@Override
public void detachFromWindow(ViewHolder holder) {}
@Override
public boolean failedToRecycle(ViewHolder holder) {
return false;
}
@Override
public boolean equals(int id) {
return id == mIdentifier;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AbstractItem<?, ?> that = (AbstractItem<?, ?>) o;
return mIdentifier == that.getIdentifier();
}
@Override
public int hashCode() {
return Long.valueOf(mIdentifier).hashCode();
}
protected static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View view) {
super(view);
}
}
}
为了简化它,我删除了注释和不必要的设置器。可以在此处的 FastAdapter
s 示例源中找到完整示例:
https://github.com/mikepenz/FastAdapter/blob/develop/app/src/main/java/com/mikepenz/fastadapter/app/items/RealmSampleUserItem.java
如果您有兴趣,可以在这里找到默认的 AbstractItem
实现:
https://github.com/mikepenz/FastAdapter/blob/develop/library-core/src/main/java/com/mikepenz/fastadapter/items/AbstractItem.java