我试图在Android SQLite中设计和实现文件夹树结构,借助Google在I/O 2017中引入的Android Room Persistence(一种ORM).在我的设计中,一个文件夹可以包含另一个文件夹和文件.这是我的文件夹和文件的代码:
文件模型:
@Entity(tableName = "files", foreignKeys = @ForeignKey(entity = Folder.class,
parentColumns = "id",
childColumns = "parent_id",
onDelete = CASCADE))
public class File {
@PrimaryKey(autoGenerate = true)
private int id;
private String title;
private Date creationDate;
@ColumnInfo(name = "parent_id")
public int parentId;
//here setters and getters skipped but exist in original code
}
Run Code Online (Sandbox Code Playgroud)
这是文件夹代码:
@Entity(tableName = "folders", foreignKeys = @ForeignKey(entity = Folder.class,
parentColumns = "id",
childColumns = "parent_id",
onDelete = CASCADE,onUpdate = SET_NULL))
public class Folder { …Run Code Online (Sandbox Code Playgroud)