微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android Room查询对象具有至少一个存在于另一个列表中的项目

如何解决Android Room查询对象具有至少一个存在于另一个列表中的项目

让我先显示一些代码。我在数据库中有症状对象的列表:

 (function() {
                let gallerys = document.querySelectorAll('.gallery');
                gallerys.forEach(gallery => {
                    updategalleryPictures(gallery);
                });
            })();

            function updategalleryPictures(gallery) {
                // Get gallery's thumbnail images
                let thumbnails = gallery.querySelectorAll('.thumbnail');
                // Change the background-image property on click
                thumbnails.forEach(thumbnail => {
                    thumbnail.addEventListener('click',() => {
                        updateMainImage(gallery,thumbnail.src)
                    });
                });
                // Initialize background-image property using the 1st thumbnail image
                let firstThumbnail = thumbnails[0];
                if (firstThumbnail === null || firstThumbnail === undefined) return;
                updateMainImage(gallery,firstThumbnail.src)
            }

            function updateMainImage(gallery,src) {
                // Get main image and check if it exists
                let mainImage = gallery.querySelector('.main-image');
                if (mainImage === null) return;
                mainImage.style.backgroundImage = 'url(' + src + ')';
            }

我在数据库中也有疾病对象的列表:

val symptomA = Symptom(id = 0,name = "SymptomA")
val symptomB = Symptom(id = 1,name = "SymptomB")
val symptomC = Symptom(id = 2,name = "SymptomC")
val symptomD = Symptom(id = 3,name = "SymptomD")

我正在使用某些类:

疾病

val diseaseA = disease(id = 0,name = "diseaseA",listofSymptoms = listof(0,1))
val diseaseB = disease(id = 1,name = "diseaseB",listofSymptoms = listof(1,2,3))
val diseaseC = disease(id = 2,name = "diseaseC",2))
val diseaseD = disease(id = 3,name = "diseaseD",listofSymptoms = listof(3))

SymptomsDao

@Entity(tableName = "diseases")
data class disease(
    @PrimaryKey @NotNull val id: Int,@NotNull val name: String,@ColumnInfo(name = "symptoms_ids") val symptomsIds: String,...)

转换器(在db类中用于将String转换为列表)

@Dao
interface diseaseDao {

    @Query("SELECT * FROM diseases WHERE id LIKE :id LIMIT 1")
    fun getdisease(id: Int): disease

    @Query("SELECT * FROM diseases")
    fun getAlldiseases(): LiveData<List<disease>>

    @Query("SELECT * FROM diseases WHERE symptoms_ids IN (:symptoms)")
    fun getdiseasesWithSymptoms(symptoms: List<Int>): LiveData<List<disease>>
    // The query from question ^
}

问题

是否可以通过病房在列表中查询至少具有一个症状ID的所有疾病?我使用的是MVVM模式,是否可以在DAO中完成,还是应该在Repository或viewmodel类中创建函数

一个示例:

input = class Converter { @TypeConverter fun fromString(stringListString: String) = stringListString.split(";").map { it.toInt() } @TypeConverter fun toString(stringList: List<Int>) = stringList.joinToString(";") }

result = listof(0)

第二个示例:

input = listof(diseaseA,diseaseC)

result = listof(1,2)

解决方法

您将不得不在ViewModel中执行此操作,因为SQL / Room无法理解List类型,因此无法为您执行这种检查。

fun getDiseasesWithSymptoms(symptoms: List<Int>): LiveData<List<Disease>> {
    return Transformations.map(diseaseDao.getAllDiseases()) { diseases ->
        diseases.filter { disease ->
            disease.listOfSymptoms.any { symptoms.contains(it)}
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。