In my app Im using a LinkhedHashSet instead of an ArrayList to hold ItemCategory objects (POJOs with name and id properties) Now I want to load this LinkedHashSet in a Spinned without having to convert the LinkedHashsSet to an ArrayList beforehand. Is it possible? Here is my code
public static void loadSpinnerData(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);
}
}
btw I've overriden the toString() method in ItemCategory object to return the category name:
public class ItemCategory {
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;
}
}
Don't use an ArrayAdapter. Instead, extend from BaseAdapter. You'll have to duplicate some of the behavior of ArrayAdapter, but you won't be hampered by its restrictions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With