我正在创建一个应用,用户可以为您拥有的特定资产(比特币,莱特币,银行帐户等)注册一组收支,为此您必须为每个资产创建一个帐户。现在,我为每个帐户都有一个cardView,您可以在其中查看当月余额。我需要在RecyclerView Adapter的帐户表和“记录(收入和支出)”表中的数据中填充此卡。这是一张图片:
我该怎么办?
我正在使用Room库,LiveData和ViewModel组件。
答案 0 :(得分:1)
如果可以将帐户,收入和费用表合并在一起,则可以通过创建一个包含结果表列的数据类来解决该问题。如果存在相同名称的列,请在查询中重命名它们,并将必填字段添加到数据类中。
在这个示例中,我有一个问题表,该问题表可能有很多答案,但是由于某些原因,我需要在recycleview中显示带有问题标题的答案,因此,出于这个原因,我将子表与其父表一起加入了。
@Dao
interface EvaluationDao {
@Query("select EVALUATION.*,QUESTION.BRIEF_DESCRIPTION,QUESTION.BRIEF_DESCRIPTION_ENG ,QUESTION.CATEGORY_ID,QUESTION.ID AS Q_ID ,QUESTION.HELP_LINK ,QUESTION.QUESTION_TITLE,QUESTION.QUESTION_TITLE_ENG ,QUESTION.WEIGHT from EVALUATION INNER join QUESTION on QUESTION.ID= EVALUATION.QUESTION_ID where EVALUATION.CLINICAL_UNIT_ID = :unitId AND QUESTION.CATEGORY_ID IN (select QUESTION_CATEGORY.ID from QUESTION_CATEGORY where QUESTION_CATEGORY.MODULE_NAME = :moduleName) order by QUESTION.QUESTION_TITLE_ENG")
fun getEvaluations4CinicalUnits(moduleName: String, unitId: String): Flowable<List<EvaluationWithQuestion>>
}
这是等效的数据类
@Parcelize
class EvaluationWithQuestion() : Parcelable {
@Embedded
var evaluation: Evaluation? = null
@ColumnInfo(name = "CATEGORY_ID")
var categoryId: Int = -1
@ColumnInfo(name = "QUESTION_TITLE")
var questionTitleFa: String = ""
@ColumnInfo(name = "QUESTION_TITLE_ENG")
var questionTitleEn: String = ""
@ColumnInfo(name = "HELP_LINK")
var helpLink: String = ""
@ColumnInfo(name = "WEIGHT", typeAffinity = ColumnInfo.REAL)
var weight: Double? = null
@ColumnInfo(name = "BRIEF_DESCRIPTION")
var briefDescriptionEn: String? = null
@ColumnInfo(name = "BRIEF_DESCRIPTION_ENG")
var briefDescriptionFa: String? = null
@ColumnInfo(name = "Q_ID")
var questionId: Int = -1
@Ignore
var rowNum: Int = -1
}