java - How can I refactor a method with an unknown Class as a parameter? -


i want make method reusable i'm not sure how provide parameter work in following context:

  • service.getposts() // method called, i.e. getposts() should vary depending on class parameter
  • for (post object : postresponse.getresults()) { // want post class come parameter
  • list<post> objects = postresponse.getresults(); // want class passed list come same parameter

method call:

retrievedata(mcardadapter, post.class); 

method:

private void retrievedata(final cardadapter mcardadapter, final class classparam) {         retrofitservice service = servicefactory.createretrofitservice(retrofitservice.class, retrofitservice.service_endpoint);         service.getposts()                 .subscribeon(schedulers.newthread())                 .observeon(androidschedulers.mainthread())                 .subscribe(new subscriber<postresponse>() {                     @override                     public final void oncompleted() {                         setrefreshingfalse();                         log.e("retrofitservice", "retrofit request completed!");                     }                      @override                     public final void onerror(throwable e) {                         setrefreshingfalse();                         log.e("retrofitservice", e.getmessage());                     }                      @override                     public final void onnext(postresponse postresponse) {                         if (postresponse != null) {                             log.e("retrofitservice", "returned objects: " + postresponse.getresults());                              (post object : postresponse.getresults()) {                                 log.e("retrofitservice", object.getobjectid() + ": " + object.gettext());                             }                              /*for (post post : postresponse.getresults()) {                                 mcardadapter.adddata(post);                             }*/                              setrefreshingfalse();                              mcardadapter.clear();                             list<post> objects = postresponse.getresults();                             mcardadapter.adddata(objects);                          } else {                             log.e("retrofitservice", "object returned null.");                         }                      }                  });     } 

when pass class classparam, unkown class: 'classparam' error.

service:

public interface retrofitservice {      string service_endpoint = "https://parseapi.back4app.com";  @headers({             "x-parse-application-id: asdf",             "x-parse-rest-api-key: asdf"     })     @get("/classes/post")     observable<postresponse> getposts();  } 

post object:

public class post implements serializable {      private static final string class_name = "post";      private string objectid;     private string text;      public post() {     }      public string getclassname() {         return class_name;     }      public string getobjectid() {         return objectid;     }      public void setobjectid(string objectid) {         this.objectid = objectid;     }      public string gettext() {         return text;     }      public void settext(string text) {         this.text = text;     }  } 

updated (according roman's suggestions):

enter image description here

enter image description here

you did not show postresponse code, suppose it's this:

public class postresponse {     private list<post> results;      public list<post> getresults() {         return results;     }      .... } 

then this.

first, parameterize response (to used instead of postresponse):

public class response<t> {     private list<t> results;      public list<t> getresults() {         return results;     }      .... } 

second, change main method signature using type parameter:

private <t> void retrievedata(final cardadapter mcardadapter, final class<t> classparam, datasaver<t> saver) { ... } 

inside retrievedata() method use t instead of post , response<t> instead of postresponse.

but have deal with

mcardadapter.adddata(objects); 

here, simple type parametrization not work. have use last parameter (saver) put results container:

saver.save(results); 

and in saver implementation do

public void save(list<post> posts) {     mcardadapter.adddata(posts); } 

datasaver generic well:

public interface datasaver<t> {     void save(list<t> objects); } 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -