问题描述
我正在使用Room实现本地缓存.我创建了typeconverter来将对象列表转换为json并返回.但是我从错误中检索json数据时收到映射问题:
I am implementing a local cache using Room. I have created typeconverter to convert list of objects to json and back. But I am receiving mapping issue while retrieving data from json with error:
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;
我在这里尝试了其他答案,但与该问题没有直接关联.
I tried other answers here but its not associated directly with this issue.
这是我的 typeconverter :
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>)
道课 @道 界面ImagesDao {
Dao class @Dao interface ImagesDao {
@Query("select * from DatabaseImagesEntity")
fun getImages(): List<FlickrImage>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(images: List<FlickrImage>)
}
FlickrImage类
data class FlickrImage(val title: String, val media: Media)
媒体类
data class Media(val m: String)
LatestImage类
data class LatestImages(val items: List<FlickrImage>)
如果您遇到此问题,并且知道解决方法,请告诉我.
Please let me know if you faced this issue and if you know the solution for this.
房间数据库实施
@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类.
The issue was I was saving data in the wrong entity, wrong TypeConverters and as a result, I was using the wrong Entity class at the time of database creation.
以下是我必须进行的一些更改以存储对象列表:
Here are the necessary changes I had to make to store the list of objects:
Flickr数据类
@Entity(tableName = "FlickerImage")
data class FlickrImage(
@PrimaryKey(autoGenerate = true)
val id: Int,
val title: String,
@TypeConverters(MediaConverter::class)
val media: Media)
媒体类的TypeConvertors
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 room/TypeConverter问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!