Firebase数据库在一个子系统中包含多个字符串

Firebase数据库在一个子系统中包含多个字符串,firebase,firebase-realtime-database,kotlin,hashmap,Firebase,Firebase Realtime Database,Kotlin,Hashmap,我在firebase工作了几个月,到目前为止我还没有遇到任何问题。我在使用kotlin,我的问题很简单,但我找不到出路。我在数据库中存储字符串:path“/user/friends”,但我无法更改变量名,字符串正在覆盖其自身。我的解决方案是使用“push()”直到“setValue()”,但使用此方法,我有以下firebase结构: "users": { "yvrYpjMwVSPBvMAvDGo26hPlWWQ2": { "email": "vinibarros.sp@g

我在firebase工作了几个月,到目前为止我还没有遇到任何问题。我在使用kotlin,我的问题很简单,但我找不到出路。我在数据库中存储字符串:path“/user/friends”,但我无法更改变量名,字符串正在覆盖其自身。我的解决方案是使用“push()”直到“setValue()”,但使用此方法,我有以下firebase结构:

"users": {
    "yvrYpjMwVSPBvMAvDGo26hPlWWQ2": {
        "email": "vinibarros.sp@gmail.com",
        "friends": {
            "-LfD9z6ke7FXFjxUb4td": {
                "email": "teste@gmail.com"
            },
            "-LfDA-NaAYAMoWiPhXy4": {
                "email": "teste2@gmail.com"
            }
        },
        "primeiroLogin": true,
        "stars": 0,
        "tutorialComplete": false,
        "uid": "yvrYpjMwVSPBvMAvDGo26hPlWWQ2",
        "username": "Vinicius Barros"
    }
}
"-LfD9z6ke7FXFjxUb4td": {
    "hashMap": {
        ...
    }
},
基本上是hashmap中的hashmaps ps:lol mindblow

如何将pushcode.value放入arraylist

我有一个类用户,它有一个friend变量,直到现在,这个变量的类型是hashMap。收到邮件的电话是“user.friends.value”

这起作用是因为有一个朋友

com.google.firebase.database.DatabaseException:未能将java.util.HashMap类型的值转换为字符串

当我添加多个用户时,我得到了这个错误,它引用了我的类用户,其中有一个“hashMap”。但是firebase给了我另一个hashmap而不是string。我在想:
hashMap

我试过这个:

同学们:

@Parcelize
class Friend(val hashMap: HashMap < String, String > ? ): Parcelable {
    constructor(): this(null)
}

这会不断返回null…

对于您的
好友
类,Firebase客户端会为具有以下结构的每个好友查找JSON:

"users": {
    "yvrYpjMwVSPBvMAvDGo26hPlWWQ2": {
        "email": "vinibarros.sp@gmail.com",
        "friends": {
            "-LfD9z6ke7FXFjxUb4td": {
                "email": "teste@gmail.com"
            },
            "-LfDA-NaAYAMoWiPhXy4": {
                "email": "teste2@gmail.com"
            }
        },
        "primeiroLogin": true,
        "stars": 0,
        "tutorialComplete": false,
        "uid": "yvrYpjMwVSPBvMAvDGo26hPlWWQ2",
        "username": "Vinicius Barros"
    }
}
"-LfD9z6ke7FXFjxUb4td": {
    "hashMap": {
        ...
    }
},
这是因为您正在使用属性
hashMap
定义
Friend
类,如下所示:
class-Friend(val-hashMap:hashMap?)

为了能够读取当前结构,您需要定义一个
数据类,如下所示:

data class Friend (
    val email: String? = null
}
现在
Friend
类中的
email
与JSON中的
email
属性匹配。由于
email
有一个默认值,因此
Friend
类将有一个默认的无参数构造函数,Firebase依赖它。

已解决

进入/成为朋友后 我这样做:

 var friends : ArrayList<String> = arrayListOf()

另外:将类似JSON的结构替换为您试图读取的实际JSON(文本,无屏幕截图)。您可以通过单击溢出菜单中的“导出JSON”链接来实现这一点(⠇) 在你的车上。看看现在是不是好些了,对不起