-3

I have the following code that I want to generify:

// retrieve json data from server and display the data on a card with two lines
private void DisplayClassData(int count, final ArrayList<TwoLineSummaryCardDataObject> tlscdo,
                                 final TwoLineSummaryViewAdapter tlsva) {

    final String url = "https://jsaonurl.com/jsondata";

    // want to generify this so that I can pass any class. But not just *any* class. Just some of my
    // classes that I have set up.
    final GsonRequest gsonRequest = new GsonRequest(url, Myclass1.class, null, new Response.Listener<Myclass1>() {

        @Override
        public void onResponse(Myclass1 myclass1) { // again, this refers back to the line above
            tlscdo.clear();
            Realm realm = Realm.getInstance(act);
            realm.beginTransaction();
            // all of my classes will have a getElement() method which will return a list
            // of objects. Perhaps I need to make a prototype and use implements???
            for (int i = 0; i < myclass1.getElement().size(); i++) { 
                tlscdo.add(new TwoLineSummaryCardDataObject(myclass1.getElement().get(i)));
                realm.copyToRealmOrUpdate(myclass1.getElement().get(i));
            }
            realm.commitTransaction();
            tlsva.notifyDataSetChanged();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            if(volleyError != null) Log.e("MainActivity", volleyError.getMessage());
            Toast.makeText(MainActivity.this, "Error in Volley", Toast.LENGTH_SHORT).show();
        }
    }
    );

    // Add the request to the queue
    Volley.newRequestQueue(this).add(gsonRequest);
    VolleyHelper.getInstance(getApplicationContext()).addToRequestQueue(gsonRequest);

}

In my data structure, I have seven classes that are all set up the same. How can I set it up so that I can reuse the code and pass any of the classes to it?

MrGibbage
  • 2,644
  • 5
  • 39
  • 50

3 Answers3

0

I think you probably want to make a factory class of some kind that you could invoke myFactory.getAllElements(Class<? extends MyBaseClass> clazz) on - assuming you have a MyBaseClass that MyClass1 etc extend.

dcsohl
  • 7,186
  • 1
  • 26
  • 44
0

You can implement a custom request. Extend the Request<T> class, where <T> represents the type of parsed response. In the parseNetworkResponse method you can instance one of the seven classes. T should be the parent class of all your seven classes.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
chiarotto.alessandro
  • 1,491
  • 1
  • 13
  • 31
0

If you want to pass a class, then, well, pass a Class. Java Class<T> object, that is.

You would need to use the superclass or an interface of your view adapters, and change your method signature as follows:

private <T> void DisplayClassData(
    int count
,   final ArrayList<T> data
,   final BaseViewAdapter va
,   Class<T> itemClass
)

Use itemClass to create new instances of the class. Use base view adapter to send notifications as needed. Use itemClass in place of Myclass1.class.

The caller would then call

DisplayClassData(tlsCount, tlscdo, tlsva, TwoLineSummaryCardDataObject.class);
DisplayClassData(esCount, escdo, esva, ExpandedSummaryCardDataObject.class);
... // And so on
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523