简体   繁体   中英

Android ExpandableListview with Room database

I need to populate an ExpandableListView whose data is fetched from Room database.

There are answers on how to do this with SQLiteDatabase :

1) Android ExpandableListView and SQLite Database

2) Android ExpandableListView From Database

Is it possible to achieve the same with Room Database?

I have two tables: a) GroupHeader b) GroupContent

@Entity
public class GroupHeader implements Serializable {
    @PrimaryKey(autoGenerate = true)
    private int groupId;
    private String groupName;
    private String otherProperty1;
    private String otherProperty2;
    /* getters and setters */
}

@Entity
public class GroupContent implements Serializable {
    @PrimaryKey(autoGenerate = true)
    private int contentId;
    private int groupId;
    private String contentName;
    private String otherProperty3;
    private String otherProperty4;    
    /* getters and setters */
}

Any suggestions please?

Found the solution using @Relation .

Reference: https://developer.android.com/reference/android/arch/persistence/room/Relation

@Entity
public class GroupHeader implements Serializable {
    @PrimaryKey(autoGenerate = true)
    private int groupId;
    private String groupName;
    private String otherProperty1;
    private String otherProperty2;
    /* getters and setters */
}

@Entity
public class GroupContent implements Serializable {
    @PrimaryKey(autoGenerate = true)
    private int contentId;
    private int groupId;
    private String contentName;
    private String otherProperty3;
    private String otherProperty4;    
    /* getters and setters */
}

public class Wrapper {
    @Embedded
    private GroupHeader header;
    @Relation(parentColumn="groupId", entityColumn="groupId", entity=GroupContent.class)
    private List<GroupContent> contents;
    /*getters & setters*/
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM