Class java.util.Map has generic type parameters, please use GenericTypeIndicator instead
The error points out correctly where you are going wrong
Map<String, String> map = dataSnapshot.getValue(Map.class);
Map class uses parameter to define the types of Key and Object where as you don't give them and simply use Map.class
which fails.
Try the below code - since Key are always string and we can have any type of Object for them
Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();
DatabaseException: Class java.util.List has generic type parameters, please use GenericTypeIndicator instead
You cannot convert any object to generic List object. You need to define which type of objects the list accepts.
Firebase provides a GenericTypeIndicator to resolve this.
Usage:
GenericTypeIndicator<List<String>> typeIndicator = new GenericTypeIndicator<List<String>>() {};
And then pass typeIndicator as the type argument in your getValue()
E g.
list = ds.getValue(typeIndicator)
Should solve your problem.
Thanks
Android, Kotlin: Creating a GenericTypeIndicator from reflection
I got the detection of the Map variable working by doing an isAssignableFrom, and creating an object for GenericTypeIndicator.
So the only thing that needed to be changed is the when
part in the code to this:
when {
field.type == Translation::class.java ->
field.set(obj, Translation(it.getValue(String::class.java)!!))
Map::class.java.isAssignableFrom(field.type) ->
field.set(obj, it.getValue(object: GenericTypeIndicator<HashMap<String, Any>>(){}))
else -> field.set(obj, it.getValue(field.type))
}
Firebase only accepts String as keys, so we can just make that one fixed, It isn't fond of using * notation for the value though, but we can just make it Any and it seems to work fine.
I hope this helps other people who might ever come across this problem x)
Related Topics
Get the Distance Between Two Geo Points
How to Add Multiple Widgets in the Same App
Android Contacts Display Name and Phone Number(S) in Single Database Query
Generate Signed APK Android Studio
Change the Font of Tab Text in Android Design Support Tablayout
What's Better? Notifydatasetchanged or Notifyitemchanged in Loop
Error Loading the Sdk When Eclipse Starts
How to Auto-Start an Android Application
Navigationview Menu Items with Counter on the Right
Android - Switch Tabs from Within an Activity Within a Tab
Android: Test Push Notification Online (Google Cloud Messaging)
Upload a File Through an Http Form, via Multipartentitybuilder, with a Progress Bar
How to Check If Receiver Is Registered in Android
Remove Application Icon and Title from Honeycomb Action Bar
How to Send Objects Through Bundle
Hw Accelerated Activity - How to Get Opengl Texture Size Limit
How to Remove All Listeners Added with Addtextchangedlistener