我有下面的代碼,我想泛型化:如何將類(不是類實例)傳遞給方法?
// 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);
}
在我的數據結構,我有七個類,都設置相同。我如何設置它以便我可以重用代碼並將任何類傳遞給它?
什麼是 「私人無效」 是什麼意思?爲什麼在虛空之前? –
MrGibbage
@MrGibbage這就是你如何定義[通用方法在某些類''](http://stackoverflow.com/a/450874/335858)。 –
dasblinkenlight