我正在尝试使用Mike Penz Fastadapter。根据ReadMe的说法,第一步是拥有一个模型类,它从他的库中扩展"AbstractItem“。我在我的Room实体类上这样做了,因为这是我想要在recyclerview中拥有的项目:
@Entity(tableName = "Category")
public class Cat extends AbstractItem<Cat, Cat.ViewHolder> {
@PrimaryKey
@NonNull
@ColumnInfo(name = "CID")
public String uid;
public String getID() { return this.uid; }
@ColumnInfo(name = "HeadID")
public String iHeaduid;
//...various getters and setters...
public Cat() {}
public Cat(String sNameP, int iCatLevelP, Cat oHeadCatP)
{
this.uid = UUID.randomUUID().toString();
this.sName = sNameP;
this.oSubCatList = new ArrayList<Cat>();
this.iCatLevel = iCatLevelP;
this.oHeadCat = oHeadCatP;
if (this.oHeadCat != null) {
this.sHeadName = oHeadCatP.sName;
this.iHeaduid = oHeadCatP.uid;
}
else
this.sHeadName = null;
}
// Methods that implement AbstractItem - I already set them on ignore...
@Ignore
@Override
public int getType() {
return R.id.textViewCat;
}
@Ignore
@Override
public int getLayoutRes() {
return R.layout.catrecyclerview_item;
}
@Ignore
@Override
public void bindView(ViewHolder holder) {
super.bindView(holder);
}
protected static class ViewHolder extends RecyclerView.ViewHolder{
protected TextView oTextView;
public ViewHolder(View itemView) {
super(itemView);
oTextView = itemView.findViewById(R.id.textViewCat);
}
}
}我的实体以前工作得很好,现在抛出了编译错误:

在扩展实体上使用房间时似乎存在问题,正如相关question所建议的那样。这是一个相当令人沮丧的问题,因为Fastadapter对于N级扩展来说是一个很好的解决方案。有没有办法解决这个问题?我可以将房间与Fastadaper一起使用吗?
我可以将项目列表复制到非数据库的虚拟模型类中,但这对我来说似乎效率很低,并且会添加臃肿的代码来使数据库与虚拟模型同步。
欣赏的想法:-)
发布于 2018-09-02 06:02:17
我发现了问题所在:它是DAO中的接收类。如果扩展了model类,则必须将超类的其他字段设置为column info或获取@Ignore标记,否则它们是不完整的Room实体,扩展的类将无法接收Room查询。
然而,AbstractItem是一个只读的库类。因此,我将内容复制到一个不同名称的类中("MyAbstractItem“f.i.)。并将@Ignore标签放在附加字段上。房间不再抱怨了。
我不知道是否有更好的解决方案,你知道吗?
编辑:很好的答案:
Android Room: Is it possible to use bounded type parameters in an entity?
https://stackoverflow.com/questions/52105473
复制相似问题