简体   繁体   中英

Use of GenericTypeindicator when retrieving data from Firebase android

I'm using the following code snippet to retrieve value from the Firebase database in android. The code is working but i do not understand how, can anyone explain to me why i have to use the 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);
   }
}

Any Help would be Appreciated :)

The Documentation for the class does a pretty good job for explaining as to why you have to use it.

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

Due to the way that Java implements generics (type-erasure), it is necessary to use a slightly more complicated method to properly resolve types for generic collections at runtime. To solve this problem, Firebase accepts subclasses of this class in calls to getValue (DataSnapshot.getValue(GenericTypeIndicator), MutableData.getValue(GenericTypeIndicator)) and returns a properly-typed generic collection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM