在Android Studio中构建项目时,我收到以下错误
错误:不确定如何将游标转换为此方法的返回类型(com.madstone.tageditor.database.TrackFileAndSong).LiveDatagetTrackFileAndSong();
我正在尝试使用中间数据类TrackFileAndSong从房间数据库中获取相关的TrackFile和Song对象,并将返回的对象存储为LiveData
曲目文件:
@Entity(tableName = "track_file_table")
public class TrackFile {
@PrimaryKey(autoGenerate = true)
public long id;
@ColumnInfo(name = "track_name")
public String trackFileName;
@ColumnInfo(name = "track_path")
public String trackFilePath;
}
歌曲:
@Entity(tableName = "song_table")
public class Song {
@PrimaryKey(autoGenerate = true)
public long songId;
@ColumnInfo(name = "Title")
public String songTitle;
@ColumnInfo(name = "Artist")
public String songArtist;
@ColumnInfo(name = "Album Artist")
public String songAlbumArtist;
@ColumnInfo(name = "Album")
public String songAlbum;
@ColumnInfo(name = "Year")
public String songYear;
public long fileId;
}
TrackFileAndSong:
public class TrackFileAndSong {
@Embedded
public TrackFile trackFile;
@Relation(
parentColumn = "id",
entityColumn = "fileId"
)
public Song song;
}
在此查询中抛出错误
@Transaction
@Query("SELECT * FROM song_table ORDER BY Title ASC")
LiveData<List<TrackFileAndSong>> getTrackFileAndSong();
如有任何帮助,我们将不胜感激
1条答案
按热度按时间cyvaqqii1#
原因是你所说的话大致如下:
从Song_table中获取trackFileAndSong列表,这等同于从Song_table中获取所有TrackFiles(Parents Ak Embedded)(然后从Song_table中获取歌曲,子对象又称为@Relation)
因此,要使用TrackFileAndSong,那么您应该使用
FROM track_file_table
,当然,ORDER BY子句没有标题列。因此,以下内容至少可以绕过错误:
然而,它可能是您真正想要的(例如)
在这种情况下,下列措施将会奏效: