0

I'm new to Android. I have a task that download generic object from a server api and save it to a specific Object at runtime inside a loop.

Example: I have an Objects of Car, Bottle, Animal.

It is it possible to change this DownloadCallback<Object> during runtime?

  while (!finish) {
     retrofitHandler.downloadData("accounts", "1", new DownloadCallBack<Object>() {
         @Override
         public void onResponse(GenericResponse<Object> response) {

         }
         @Override
         public void onFailure(String response) {

         }
       });
  }

I know the Generic Class in set at Compile Time. but is this possible?

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • i don't think that's possible. Instead, you can just use object type and cast it to custom object in runtime. – Mehran Zamani Oct 11 '17 at 11:45
  • @MehranZamani, thanks for your comments. I heard of Reflection. do you think it is possible to that approach? – Daryll Mendoza Sabate Oct 17 '17 at 01:22
  • I haven't been worked with Reflection so don't know if it's going to help you with that. there are some answers in SO that you can check out. [Link1](https://stackoverflow.com/questions/11164756/reflection-for-class-of-generic-parameter-in-java) and [Link2](https://stackoverflow.com/questions/1901164/get-type-of-a-generic-parameter-in-java-with-reflection) and [Link3](https://stackoverflow.com/questions/3403909/get-generic-type-of-class-at-runtime). – Mehran Zamani Oct 17 '17 at 04:49
  • @MehranZamani Thanks man. – Daryll Mendoza Sabate Oct 18 '17 at 11:13
  • I made the changes on my API. which will now return one object(Generic). then on Android. i'll consume it as List> but it takes a lot of time when iterating the data. – Daryll Mendoza Sabate Oct 18 '17 at 11:15

0 Answers0