M.U
M.U

Reputation: 401

Android room, List within entity?

I have the following Entity :

public class SubItem
{
    //....code
    Location loc;
}
@Entity(tableName = "Item_table")
public class Item
{
    @PrimaryKey(autoGenerate = true)
    public long id;
    public String Name;
    //...code
    List<SubItem> subItems;
}

the app consist of a RecyclerView that displays Items names only, and when a Item is pressed it should display the subItems in a different activity, so I only need to load all the information for each item except it's subItems, but in the same time I may need to be able to query the Items based on their subItems properties such as 'Location', What is the correct way to do that given that the given code does't work ? I think this answers why the code does't work, but I don't understand the proposed solution ?

Upvotes: 4

Views: 3090

Answers (1)

MohammadL
MohammadL

Reputation: 2458

You need to create two tables Item_Table and SubItem_Table and set a relation between them with a ForeignKey. See this example: https://android.jlelse.eu/android-architecture-components-room-relationships-bf473510c14a

Upvotes: 3

Related Questions