com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为Data类对象KOTLIN [英] com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type Data class object KOTLIN

查看:44
本文介绍了com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为Data类对象KOTLIN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还阅读了有关同一问题的其他评论,但没有人提及我的情况

I have read other comments on this same issue, but none of them has touched on a situation like mine

在我的下面,介绍了数据的结构方式

In mine, below describes how the data is structured

      val ref: DatabaseReference? = Firebase.database.getReference("symbols/${alphabets}")

{
"symbols" : {
   "alphabets" : {
   "a" : {
       "available" : true,
       "text" : "A",
       "timestamp" : 1.512686825309134E9
   },
   "b" : {
       "available" : true,
       "text" : "B",
       "timestamp" : 1.512687248764272E9
   }

"NameOfSymbols" : "group of alphabets"
   }
}
}

数据列表继续,然后字符串在对象的末尾

the list of the data continues, then the string is at the end of the objects

*我的之所以显示错误,是因为它无法转换字符串"NameOfSymbols".:字母";到数据类中指定的对象

*The reason why mine is showing the error is because it can't convert the string "NameOfSymbols" : "alphabets" to the objects as specified in the data class

那么,我可以使用Kotlin

有没有一种方法可以排除那部分children值,而只获得数据类中指定的那一部分?

数据类

data class alphabets(
    val name: Names,
var NameOfSymbols: String? = null) {
    data class Names(

        var available: Boolean? = null,
        var text: String? = null,
        var timestamp: Long? = null) {

    }
}

推荐答案

如果您正在听

"symbols/alphabets"

那么里面的孩子是:

"a" : {
   "available" : true,
   "text" : "A",
   "timestamp" : 1.512686825309134E9
}

这是一个 Map< String,Any> ,在这种情况下,我们可以根据需要将 Any 定义到特定的 data类

That is a Map<String, Any> in this case we can define that Any into a specific data class as you want

data class Names(
    val available: Boolean? = null,
    val text: String? = null,
    val timestamp: Long? = null
)

然后,您必须将快照转换为:

Then you have that you have to transform your snapshot to:

Map<String, Names>

这篇关于com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为Data类对象KOTLIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆