房间:错误:不确定如何将游标转换为此方法的返回类型(void)



我设置了此查询以返回这些表中的所有记录,并在 android 的回收器视图上显示信息。 数据库是使用房间持久性库(又名 SQLITE(设置的。

@Query
("SELECT moodBeforetable.userId, 
moodBeforetable.moodBefore, 
moodBeforetable.cbtId, 
cbtTable.automaticThought, 
cbtTable.twistedThinkingPK, 
cbtTable.challengeThought, 
cbtTable.rationalThought,  
cbtTable.date, 
moodAfterTable.moodAfter, 
twistedThinkingTable.twistedThinkingPK,
twistedThinkingTable.allOrNothing,  
twistedThinkingTable.blamingOthers,
twistedThinkingTable.catastrophizing, 
twistedThinkingTable.emotionalReasoning, 
twistedThinkingTable.fortuneTelling, 
twistedThinkingTable.labelling, 
twistedThinkingTable.magnifyingTheNegative, 
twistedThinkingTable.mindReading, 
twistedThinkingTable.minimisingThePositive,
twistedThinkingTable.overGeneralisation, 
twistedThinkingTable.selfBlaming, 
twistedThinkingTable.shouldStatement 
FROM moodBeforetable
JOIN cbtTable ON moodBeforetable.cbtId = cbtTable.cbtId
JOIN twistedThinkingTable ON cbtTable.cbtId = twistedThinkingTable.cbtId
JOIN moodAfterTable ON moodAfterTable.cbtId = cbtTable.cbtId
WHERE moodBeforetable.date >= datetime('now', '-1 year')
AND moodBeforetable.userId = :userId 
ORDER BY :date DESC")
LiveData<List<MoodBeforeTable>> moodLogsAll (int userId, String date);

当我尝试编译应用程序时,出现以下错误:

该查询返回一些未被 com.example.feelingfit.persistence.tables.MoodBeforeTable 使用的列。 您可以使用字段上的@ColumnInfo注释来指定映射。

谁能帮我调试一下,找出为什么应用程序无法编译?

问题是Room无法将自定义查询的结果映射到现有MoodBeforeTable。这是因为您的返回类型是List<MoodBeforeTable>,但您使用了使用TwistedThinkingTableMoodAfterTable等的连接。

您应该做的是创建一个新的POJO,如下所示:

public class MoodLogPojo() {
private int userId;
private String moodBefore;
zprivate int cbtId;
private String automaticThought;
private int twistedThinkingPK;
private String challengeThought;
private String rationalThought;
private String date;
private String moodAfter;
private int twistedThinkingPK;
private String allOrNothing;
private String blamingOthers;
private String catastrophizing;
private String emotionalReasoning;
private String fortuneTelling;
private String labelling;
private String magnifyingTheNegative;
private String mindReading;
private String minimisingThePositive;
private String overGeneralisation;
private String selfBlaming;
private String shouldStatement;
// generate getters and setters too
public void setSelfBlaming(Stirng selfBlming) {
this.selfBlming = selfBlming
}
public String getSelfBlaming() { return selfBlaming; }
// and so on ...
}

然后使用此类作为返回类型,如下所示:

LiveData<List<MoodLogPojo>> moodLogsAll (int userId, String date);.

注意:请注意MoodLogPojo类。根据每个Entity的相应数据类型对其进行修改。

最新更新