Issues when using Android Room together with Mike Penz Fastadapter

211 Views Asked by At

I am trying to use Mike Penz Fastadapter. According to the ReadMe, the first step is to have a model class that extends "AbstractItem" from his library. I did that on my Room entity class, because that is the item that I want to have in the 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);
        }
    }
}

My entity that used to work just fine now throws compile errors:

enter image description here

It seems like there are issues when using Room on extended entities, as suggested by this related question. This is quite a bummer, since Fastadapter would have been a nice solution for N-level expandables. Any idea on how to tackle this problem? Can I use Room with Fastadaper?

I could copy the list of items into a non-database dummy model class, but that seems quite inefficient to me and would add bloat code to synchronise the database with the dummy...

Ideas appreciated :-)

1

There are 1 best solutions below

0
On

I found out what the problem was: It was the receiving class in the DAO. If the model class is extended, additional fields of the superclass must be set as column info or get an @Ignore tag, otherwise they are an incomplete Room entity and the extended class wont be able to receive the Room query.

AbstractItem though is a library class and readonly. So I copied the content in a class with a different name ("MyAbstractItem" f.i.) and put @Ignore tags on the additional fields. Room stopped complaining.

I dont know if there is a more elegant solution, do you know one?

Edit: Good answer here:

Android Room: Is it possible to use bounded type parameters in an entity?