简体   繁体   English

Android/Room/ViewModel:将 ViewModel 保存到房间数据库时出错

[英]Android/Room/ViewModel : Error while saving ViewModel to Room Database

I have an activity which uses a ViewModel class to store and manage UI-related data.我有一个使用 ViewModel class 来存储和管理 UI 相关数据的活动。 The view model class used in the activity has a structure similar to the one given below:活动中使用的视图 model class 具有类似于下面给出的结构:

class SomeViewModel:ViewModel(){
  @PrimaryKey(autoGenerate=true)
  var id=0

  var field1:Array<Double?>=arrayOf(null,null)
  var field2:Array<Double?>=arrayOf(null,null)
  var field3:Array<Double?>=arrayOf(null,null)

  @Ignore
  val someFragments=HashMap<String,Fragment>()
  @Ignore
  val someMap=HashMap<String,Int>()
}

What I am trying to do is to save the data in the view model object to the local database when back is pressed to close the activity.我要做的是在按下返回关闭活动时将视图 model object 中的数据保存到本地数据库。 But the issue is when I am trying to do so I am getting an error.但问题是当我试图这样做时,我得到了一个错误。

Cannot figure out how to save this field into database.无法弄清楚如何将此字段保存到数据库中。 You can consider adding a type converter for it.您可以考虑为其添加类型转换器。 - mBagOfTags in androidx.lifecycle.ViewModelerror: Cannot find getter for field. - androidx.lifecycle.ViewModelerror 中的 mBagOfTags:找不到字段的 getter。 - mBagOfTags in androidx.lifecycle.ViewModelerror: Cannot find getter for field. - androidx.lifecycle.ViewModelerror 中的 mBagOfTags:找不到字段的 getter。 - mCleared in androidx.lifecycle - mCleared 在 androidx.lifecycle

I am using a TypeConverter identical to the one given below:我使用的 TypeConverter 与下面给出的相同:

class Converter{
    @TypeConverter
    fun fromArrayDouble(field:Array<Double?>):String{
        val s=StringBuilder("")
        var first=true
        for(k in field){
            s.append(k.toString())
            if(first){
                s.append(",")
                first=false
            }
        }
        return s.toString()
    }

    @TypeConverter
    fun fromString(str:String):Array<Double?>{
        val parts=str.split(',')
        val res=ArrayList<Double?>()
        for(p in parts){
            try{
                res.add(p.toDouble())
            }catch(e:Exception){
                res.add(null)
            }
        }
        return res.toTypedArray()
    }
}

and a database class similar to:和数据库 class 类似于:

@Database(entities=[SomeViewModel::class], version=1, exportSchema=false)
@TypeConverters(Converter:class)
abstract class SomeDatabase:RoomDatabase(){
  // database definition
}

I don't understand where I am going wrong.我不明白我哪里错了。 Any help will be highly appreciated.任何帮助将不胜感激。

Please refer to this answer from another post.请参阅另一篇文章中的此答案。

Kotlin room does not allow defining variables in entity class start of 'i' character. Kotlin 房间不允许在实体 class 中以“i”字符开头定义变量。 I had the same error.我有同样的错误。 I solved it, albeit difficult.我解决了,虽然很困难。 Replace id with pId and it will be fine.用 pId 替换 id 就可以了。

Or probably you need to add getter to be found by Room.或者您可能需要添加 getter 才能被 Room 找到。

fun getId(): Int {
    return id
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM