【问题标题】:Use of GenericTypeindicator when retrieving data from Firebase android从 Firebase android 检索数据时使用 GenericTypeindicator
【发布时间】:2017-04-20 12:31:08
【问题描述】:

我正在使用以下代码 sn-p 从 Android 中的 Firebase 数据库中检索值。代码正在运行,但我不明白如何,谁能向我解释为什么我必须使用 GenericTypeindicator。

dbRef.addValueEventListener(){
   public void onDataChange(DataSnapshot ds) {
     GenericTypeIndicator<Map<String, String>> gType = new GenericTypeIndicator<Map<String, String>>() {
     };
     Map<String, String> myMap = ds.getValue(gType);
   }
}

任何帮助将不胜感激:)

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    该类的文档很好地解释了为什么必须使用它。

    https://www.firebase.com/docs/java-api/javadoc/com/firebase/client/GenericTypeIndicator.html

    由于 Java 实现泛型的方式(类型擦除),有必要使用稍微复杂一点的方法在运行时正确解析泛型集合的类型。为了解决这个问题,Firebase 在调用 getValue (DataSnapshot.getValue(GenericTypeIndicator), MutableData.getValue(GenericTypeIndicator)) 时接受这个类的子类,并返回一个类型正确的泛型集合

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多