Reputation: 14711
In my android application I have ItemCategory POJO and ItemSubcategory POJO that both extend Category POJO holding some common characteristics.
I have a custom Load Spinner method that I pass an ArrayList of Objects to, and it loads them into a spinner (the Android name for drop-down/select)
I tried passing an ArrayList to a single method loadCategoriesIntoSpinner( that accepts an ArrayList as a parameter) and I expected it to sort out that what Im passing is an ArrayList but it didnt.
public static void loadCategoriesIntoSpinner(Context context, ArrayList<ItemCategory> array, Spinner spinner) {
// Creating adapter for spinner
ArrayAdapter<ItemCategory> dataAdapter = new ArrayAdapter<ItemCategory>(context,
R.drawable.simple_spinner_item, array);
// Drop down layout style - list view with radio button
dataAdapter
.setDropDownViewResource(R.drawable.simple_spinner_dropdown_item);
// Attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
public static void loadSubcategoriesIntoSpinner(Context context, ArrayList<ItemSubcategory> array, Spinner spinner) {
// Creating adapter for spinner
ArrayAdapter<ItemSubcategory> dataAdapter = new ArrayAdapter<ItemSubcategory>(context,
R.drawable.simple_spinner_item, array);
// Drop down layout style - list view with radio button
dataAdapter
.setDropDownViewResource(R.drawable.simple_spinner_dropdown_item);
// Attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
Here are the Category objects:
Main class Category:
abstract public class Category {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return this.name;
}
} // End of Class
inheriting class ItemCategory
public class ItemCategory extends Category {
private String logo;
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
} // End of Class
inheriting class ItemSubcategory:
public class ItemSubcategory extends Category {
private int parentID;
public void setParentID(int parentID) {
this.parentID = parentID;
}
public int getParentID() {
return parentID;
}
} // End of Class
Upvotes: 0
Views: 102
Reputation: 27984
public static void <T extends Category> loadCategoriesIntoSpinner(Context context, List<T> array, Spinner spinner) {
ArrayAdapter<T> dataAdapter = new ArrayAdapter<T>(context, R.drawable.simple_spinner_item, array);
...
Upvotes: 2
Reputation: 25873
public static void loadSubcategoriesIntoSpinner(Context context, List<? extends Category> array, Spinner spinner)
or also
public static void loadSubcategoriesIntoSpinner(Context context, List<Category> array, Spinner spinner)
if you create your list as List<Category>
.
I suggest you use the interface List
instead of a specific implementation like ArrayList
. This way you can change the implementation (to LinkedList
for example) without having to modify your code.
Upvotes: 3