My app needs to be able to change the drawables displayed in a gridview, mainly just colours and some of the information displayed. The different styles have different .java and XML files.
The different .java files are used for displaying different types of the same thing, and so inherit from a shared parent, for example basicrunner extends runner and lollipoprunner extends runner.
The adapter extends BaseAdapter, and overrides the methods for getItem etc, but they specify one return type;
public class RunnersAdapter extends BaseAdapter {
private static final String TAG = "RunnersAdapter";
//used in switch statement to decide which XML to inflate
private static final int LayoutTYPE_1 = 1;
private static final int LayoutTYPE_2 = 2;
private static final int LayoutTYPE_3 = 3;
int layout = 2; //hard coded just for testing
@Override
public Runner getItem(int position) { //runner is superclass
return mRunners.get(mKeys[position]);
}
Is there a way to use this one RunnersAdapter for each type of runner, or am I better off making a different adapter for each type?
EDIT:
This is an upgrade from an adapter that only ever needed to display one data type, namely Runner. Now we want to display multiple different types of runners, but not necessarily make multiple types of adapters.
Some more code to illustrate what I mean:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(layout == LayoutTYPE_1){ //LollipopView
Runner runner = getItem(position); //should be a lollipopRunner!!
gridView = inflater.inflate(R.layout.lollipopRunner, null); //use lollipopRunner XML
}else if(layout == LayoutTYPE_2){
Runner runner = getItem(position); //should be basicRunner!!
gridView = inflater.inflate(R.layout.basicrunner, null); //use basic runner XML
As shown above, the overridden getItem method returns type "Runner", which is the parent class of BasicRunner and LollipopRunner.
Is there a correct application of polymorphism that would allow this to work? Do I need new adapters for each Runner?