Android Room:是否可以在实体中使用有界类型参数? [英] Android Room: Is it possible to use bounded type parameters in an entity?

查看:102
本文介绍了Android Room:是否可以在实体中使用有界类型参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将Mike Penz Fastadapter与Android Room结合在一起.可扩展模型类需要这样实现:

I am currently combining Mike Penz Fastadapter with Android Room. The expandable model class needs to be implemented like this:

public class MyClass<Parent extends IItem & IExpandable,
        SubItem extends IItem & ISubItem>
        extends AbstractExpandableItem<MyClass<Parent, SubItem>, MyClass.ViewHolder, SubItem> {

我也想将模型用作房间实体.第一个问题很容易解决-我创建了AbstractExpandableItem的自定义版本,在该字段中将使用@Ignore标签标注字段,以免阻碍代码生成.这样,Fastadapter的一个简单实现就可以很好地工作.

I want to use the model also as a room entity. The first problem was easy to solve - I created a custom version of AbstractExpandableItem where the fields would be annoteted with @Ignore tags to not brake the code generation. A simpler implementation of Fastadapter worked just fine this way.

但是,Room似乎对实体的有界类型参数存在问题,因为它在DAO实现中引发了这些编译错误:

However, Room seems to have a problem with bounded type parameters for entities, as it throws these compile errors in the DAO implementation:

  • 错误:(40,115)错误:找不到符号类父级
  • 错误:(40,123)错误:找不到符号类SubItem

我的DAO是:

    @Dao
public interface MyDAO {

    @Query("Select * from Table")
    LiveData<List<MyClass>> getAllStuff();

此人不同,我无法解决我的问题更新存在问题-我将Room gradle版本放在1.1.1上,但该错误仍然发生.

Unlike this guy, I could not solve my issues with an update - I put my Room gradle version on 1.1.1 and the error still happens.

推荐答案

FastAdapter还提供了定义常规Model类的可能性,该类可以是超简单的POJO和Item类.

The FastAdapter also offers the possibility to define a normal Model class, which can be a super simple POJO and a Item class.

这样,您可以在模型中拥有所有数据定义(不需要任何父类或实现),而在Item中进行UI绑定.

This way you can have all your data definition in the model, which will not require any parent classes or implementations, and the Item which will do the UI binding.

FastAdapter的示例应用程序中可以找到一个简单的示例.

A simple example can be found in the sample application of the FastAdapter.

您将使用ModelAdapter而不是ItemAdapter,然后提供有关模型如何转换为Item的逻辑.

Instead of an ItemAdapter you will use a ModelAdapter, and then you provide the logic on how your model converts to a Item.

这可以很简单:

ModelAdapter<IconModel, ModelIconItem> itemAdapter = new ModelAdapter<>(new IInterceptor<IconModel, ModelIconItem>() {
        @Override
        public ModelIconItem intercept(IconModel iconModel) {
            return new ModelIconItem(iconModel);
        }
});

对于这个简单的示例,模型看起来像:

For this simple sample the model looks like:

public class IconModel {
    public IIcon icon;

    public IconModel(IIcon icon) {
        this.icon = icon;
    }
}

项目看起来像这样:

public class ModelIconItem extends ModelAbstractItem<com.mikepenz.fastadapter.app.model.IconModel, ModelIconItem, ModelIconItem.ViewHolder> {
    public ModelIconItem(com.mikepenz.fastadapter.app.model.IconModel icon) {
        super(icon);
    }
    @Override
    public int getType() {
        return R.id.fastadapter_model_icon_item_id;
    }
    @Override
    public int getLayoutRes() {
        return R.layout.icon_item;
    }
    @Override
    public void bindView(ViewHolder viewHolder, List<Object> payloads) {
        super.bindView(viewHolder, payloads);

        ... bind logic
    }
    @Override
    public ViewHolder getViewHolder(View v) {
        return new ViewHolder(v);
    }
    protected static class ViewHolder extends RecyclerView.ViewHolder {
        protected View view;
        @BindView(R.id.name)
        public TextView name;
        @BindView(R.id.icon)
        public IconicsImageView image;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            this.view = view;
        }
    }
}

完整的示例代码可以在这里找到: https://github.com/mikepenz/FastAdapter/blob/develop/app/src/main/java/com/mikepenz/fastadapter/app/ModelItemActivity.java#L51

The full sample code can be found here: https://github.com/mikepenz/FastAdapter/blob/develop/app/src/main/java/com/mikepenz/fastadapter/app/ModelItemActivity.java#L51

这篇关于Android Room:是否可以在实体中使用有界类型参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆