android - 如何在 LiveData类中使用“I”作为具体类型参数?
问题描述
解决方案
类的I
类型不能被具体化,因此您不能I
为该函数创建重载的具体化。我认为您可以通过使类成为构造函数参数来手动具体化类类型。像这样:
class ItemLiveData<I>(private val type: Class<I>): LiveData<I>() {
override fun onEvent(querySnapshot: QuerySnapshot?, e: FirebaseFirestoreException?) {
if (e != null) return
for (documentChange in querySnapshot!!.documentChanges) {
when (documentChange.type) {
DocumentChange.Type.ADDED -> setValue(getItem(documentChange)) //Add to LiveData
}
}
private fun getItem(doc: DocumentChange) = doc.document.toObject<I>(type)
}
推荐阅读
- mysql - MySQL 并发更新
- android - 在某些设备(如 Google Pixel、Moto G4、OnePlus 6)中获取 LocationManager 和经纬度 null
- javascript - 使用 React 未选中复选框输入后获取初始状态
- php - 未捕获的 PHP 异常 MongoDB\Driver\Exception\AuthenticationException:“身份验证失败。”
- scala - 如何在 Map[String,(String, String)] 中访问地图的单个值
- php - 来自阵列的文件未在邮件中添加为多个附件?
- c# - C# 用户在列表中输入数组
- scala - Scala案例类,修改构造函数中的参数
- kubernetes - kops 在现有的 AWS vpc 和子网中创建集群
- javascript - React Native 中可能出现未处理的 Promise Rejection (id:0) 警告