Java 房间-比较实体中的列表
我的房间有问题。 我有一个实体:Java 房间-比较实体中的列表,java,android,kotlin,android-room,android-architecture-components,Java,Android,Kotlin,Android Room,Android Architecture Components,我的房间有问题。 我有一个实体: @Entity(tableName = "Entity") data class Entity(val recipients: List<ID>?) { @ColumnInfo(name = "id") @PrimaryKey(autoGenerate = true) var id: Long = 0 } @Entity(tableName=“Entity”) 数据类实体(val收件人:列表?){ @ColumnInfo(name=
@Entity(tableName = "Entity")
data class Entity(val recipients: List<ID>?) {
@ColumnInfo(name = "id")
@PrimaryKey(autoGenerate = true) var id: Long = 0
}
@Entity(tableName=“Entity”)
数据类实体(val收件人:列表?){
@ColumnInfo(name=“id”)
@PrimaryKey(autoGenerate=true)变量id:Long=0
}
如您所见,它包含ID列表(typealias ID=Long)
因此,我制作了TypeConverter:
@TypeConverter
fun getListOfLongs(data: String?): List<ID> {
if (data == null) {
return Collections.emptyList()
}
val listType = object : TypeToken<List<Long>>() {}.type
return gson.fromJson(data, listType)
}
@TypeConverter
fun convertListToString(ids: List<ID>): String = gson.toJson(ids)
@TypeConverter
有趣的getListOfLongs(数据:字符串?):列表{
如果(数据==null){
return Collections.emptyList()
}
val listType=对象:TypeToken(){}.type
返回gson.fromJson(数据,列表类型)
}
@类型转换器
fun convertListToString(id:List):String=gson.toJson(id)
但我有一个问题,我无法正确查询此列表:
@Query("select count(*) from Message where recipients in (:recipients)")
fun count(recipients: List<ID>?): Int
@Query(“从收件人所在的邮件(:recipients)”中选择count(*)
乐趣计数(收件人:列表?):Int
如您所见,我需要比较两个列表(实体中的列表和方法中的列表),并从方法的列表中获取包含Id的所有实体,但此查询始终返回0。我读到TypeConverter的列表可能有问题,但我找不到任何解决方案。用@RawQuery解决它:
" WHERE recipients LIKE ${recipients.joinToString(",", "\"", "\"")}"
结束编辑类型转换器:
@TypeConverter
fun getListOfLongs(data: String?): List<Long> {
if (data == null) {
return Collections.emptyList()
}
val listType = object : TypeToken<List<Long>>() {}.type
return gson.fromJson("[$data]", listType)
}
@TypeConverter
fun convertListToString(ids: List<Long>?): String? {
if (ids == null) return null
var idsString = gson.toJson(ids)
idsString = idsString.replace("]", "")
idsString = idsString.replace("[", "")
return idsString
}
@TypeConverter
有趣的getListOfLongs(数据:字符串?):列表{
如果(数据==null){
return Collections.emptyList()
}
val listType=对象:TypeToken(){}.type
返回gson.fromJson(“[$data]”,listType)
}
@类型转换器
有趣的convertListToString(ID:List?):字符串?{
如果(ids==null)返回null
var idsString=gson.toJson(ids)
idsString=idsString.replace(“]”,“”)
idsString=idsString.replace(“[”,“”)
返回字符串
}