gpt4 book ai didi

检索对象列表时出现 Android 房间/TypeConverter 问题

转载 作者:行者123 更新时间:2023-12-05 00:44:38 24 4
gpt4 key购买 nike

我正在使用 Room 实现本地缓存。我创建了类型转换器来将对象列表转换为 json 并返回。但是我在从 json 检索数据时收到映射问题,但出现错误:

The columns returned by the query does not have the fields [title,media] in 
com.example.theApp.data.FlickrImage even though they are annotated as non-null or
primitive. Columns returned by the query: [items]

另一个是这样的:
error: Cannot figure out how to read this field from a cursor.
private final com.example.theApp.data.Media media = null;

我在这里尝试了其他答案,但它与此问题没有直接关联。

这是我的 类型转换器 :
class FlickrImageConverters {

@TypeConverter
fun fromImageListToJson(stat: List<FlickrImage>): String {
return Gson().toJson(stat)
}

/**
* Convert a json to a list of Images
*/
@TypeConverter
fun fromJsonToImagesList(jsonImages: String): List<FlickrImage> {
val type = object : TypeToken<List<FlickrImage>>() {}.type
return Gson().fromJson<List<FlickrImage>>(jsonImages, type)
}

}

这是我的实体类:
@Entity
data class DatabaseImagesEntity(
@PrimaryKey
@TypeConverters(FlickrImageConverters::class)
@SerializedName("item")
val items: List<FlickrImage>)

道类
@道
接口(interface)ImagesDao {
@Query("select * from DatabaseImagesEntity")
fun getImages(): List<FlickrImage>


@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(images: List<FlickrImage>)

}

Flickr图片类
data class FlickrImage(val title: String, val media: Media)

媒体课
data class Media(val m: String)

最新图片类
data class LatestImages(val items: List<FlickrImage>)

如果您遇到此问题以及您是否知道解决方案,请告诉我。

房间数据库实现
@Database(entities = [DatabaseImagesEntity::class], version = 1, 
exportSchema = false)
@TypeConverters(FlickrImageConverters::class)
abstract class FlickrDatabase: RoomDatabase() {
abstract val imagesDao: ImagesDao
}


private lateinit var INSTANCE: FlickrDatabase


fun getDatabase(context: Context): FlickrDatabase{

synchronized(FlickrDatabase::class.java){
if(!::INSTANCE.isInitialized){
INSTANCE = Room.databaseBuilder(context.applicationContext,
FlickrDatabase::class.java,
"flickerImages").build()
}
}
return INSTANCE
}

最佳答案

问题是我将数据保存在错误的实体、错误的 TypeConverters 中,因此,我在创建数据库时使用了错误的 Entity 类。

以下是我为存储对象列表而必须进行的必要更改:

Flickr 数据类

@Entity(tableName = "FlickerImage")
data class FlickrImage(
@PrimaryKey(autoGenerate = true)
val id: Int,
val title: String,
@TypeConverters(MediaConverter::class)
val media: Media)

媒体类的类型转换器
class MediaConverter {

@TypeConverter
fun fromMediaToJson(stat: Media): String {
return Gson().toJson(stat)
}

/**
* Convert a json to a list of Images
*/
@TypeConverter
fun fromJsonToMedia(jsonImages: String): Media {
val type = object : TypeToken<Media>() {}.type
return Gson().fromJson<Media>(jsonImages, type)
}
}

DAO类
@Dao
interface ImagesDao {

@Query("select * from FlickerImage")
fun getImages(): LiveData<List<FlickrImage>>

数据库类
@Database(entities = [FlickrImage::class], version = 1, exportSchema = false)
@TypeConverters(MediaConverter::class)
abstract class FlickrDatabase: RoomDatabase() {
abstract val imagesDao: ImagesDao
}


private lateinit var INSTANCE: FlickrDatabase


fun getDatabase(context: Context): FlickrDatabase{

synchronized(FlickrDatabase::class.java){
if(!::INSTANCE.isInitialized){
INSTANCE = Room.databaseBuilder(context,
FlickrDatabase::class.java,
"flickerImages").build()
}
}
return INSTANCE
}


@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(images: List<FlickrImage>)

}

关于检索对象列表时出现 Android 房间/TypeConverter 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59887192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com