Android Room – Interface as Entity

I have an interface that contains object types. It is also used for ViewTypes.

public interface EventType {
    int TYPE_FROG = 1;
    int TYPE_STEAK = 2;
    int TYPE_ELEPHANT = 3;

    int getType();

    void onBindViewHolder(RecyclerView.ViewHolder viewHolder);
}

And also I have three classes of data – Frog, Elephant and Steak.
I display an array of all these objects on the screen.

@Entity
public class Frog implements EventType {

    @PrimaryKey(autoGenerate = true)
    int id;

    String title;

    Boolean isCompleted;

    @Override
    public int getType() {
        return EventType.TYPE_FROG;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder) {
        ViewHolderFactory.FrogViewHolder frogViewHolder = (ViewHolderFactory.FrogViewHolder) viewHolder;
        frogViewHolder.tv.setText("Жаба");
    }
}

@Entity
public class Elephant extends Frog implements EventType{

    @PrimaryKey(autoGenerate = true)
    int id;

    String title;

    Boolean isCompleted;

    @Override
    public int getType() {
        return EventType.TYPE_ELEPHANT;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder) {
        ViewHolderFactory.ElephantViewHolder elephantViewHolder = (ViewHolderFactory.ElephantViewHolder) viewHolder;
        elephantViewHolder.tv.setText("Слон");
    }
}

@Entity
public class Steak extends Frog implements EventType{

    @PrimaryKey(autoGenerate = true)
    int id;

    String title;

    Boolean isCompleted;


    @Override
    public int getType() {
        return EventType.TYPE_STEAK;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder) {
        ViewHolderFactory.SteakViewHolder steakViewHolder = (ViewHolderFactory.SteakViewHolder) viewHolder;
        steakViewHolder.tv.setText("Стейк");
    }
}

in my fragment I create a list of objects of all three types, i.e. List.
List displayed in RecyclerView using an adapter.

How can I save this List to the database? Or do I have to create a separate table from the interface?

Source: Android Questions

LEAVE A COMMENT