gpt4 book ai didi

android - Kotlin 。未经检查将 : Any? 转换为 HashMap?

转载 作者:行者123 更新时间:2023-11-30 00:04:51 25 4
gpt4 key购买 nike

我想得到 HashMap<String?, String?>?来自 firebaseDatabase :

override fun onDataChange(dataSnapshot: DataSnapshot) {
val users: HashMap<String?, String?>? = dataSnapshot.value as HashMap<String?, String?>? // todo !!!
if (users != null) {
if (!users.containsKey(userUid)) {
users[userUid] = userName
}
}
}

此代码有效但 Android Studio在第二行显示警告:

 Unchecked cast: Any? to HashMap<String?, String?>?

如何以正确的方式解决这个问题?

最佳答案

GenericTypeIndicator 的方法有效,谢谢@leoderprofi

    val ti = object : GenericTypeIndicator<HashMap<String?, String?>?>() {}
//...
override fun onDataChange(dataSnapshot: DataSnapshot) {
val users: HashMap<String?, String?>? = dataSnapshot.getValue(ti)
if (users != null) {
if (!users.containsKey(userUid)) {
users[userUid] = userName
}
}
}

关于android - Kotlin 。未经检查将 : Any? 转换为 HashMap<String?, String?>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49209150/

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