Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store a object array by using Room in Android?

Tags:

I want to cache a song-list in my app, the Song-list structure is like below:

 @Entity
 public class Songlist {
      String _id;
      String desc;
      List<SongDesc> songWithComment;
      .....

    public static class SongDesc {
      String comment;
      Song song;
    }
}
 @Entity
 pulbic class Song {
      String name;
      String type;
      ......
 }

The lib of operating sqlite3 is android.arch.persistence.room, but it dosen't allow object references in a table.Is there any way to cache a song-list by using Room in Android?

like image 796
jaime Avatar asked Dec 14 '17 14:12

jaime


People also ask

Is Android Room an ORM?

An easy way to use a database in an Android app is with a library called Room. Room is what's called an ORM (Object Relational Mapping) library, which as the name implies, maps the tables in a relational database to objects usable in Kotlin code.

What is the Room database in Android?

What is Room? Room is a persistence library that provides an abstraction layer over the SQLite database to allow a more robust database. With the help of room, we can easily create the database and perform CRUD operations very easily.


1 Answers

Also if you want to store some custom objects, you can use @Embedded annotation like in example bellow :

class Address {
public String street;
public String state;
public String city;

@ColumnInfo(name = "post_code")
public int postCode; 
}


@Entity
class User {
@PrimaryKey
public int id;

public String firstName;

@Embedded
public Address address;
} 
like image 166
C. Alen Avatar answered Sep 19 '22 13:09

C. Alen