简体   繁体   中英

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

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?

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;
} 

If you want to save an ArrayList of some type into the database, you shuld use TypeConverter to convert the ArrayList from and into a simpler recognizable type to the database engine, like a String .

See this: Android Room Database: How to handle Arraylist in an Entity? and https://commonsware.com/AndroidArch/previews/room-and-custom-types

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