Creating an ArrayAdapter <ItemCategory> accepts a LinkedHashSet instead of an ArrayList

In my Im application using LinkhedHashSet instead of ArrayList to store ItemCategory objects (POJOs with names and identifiers) Now I want to load this LinkedHashSet into Spinned without first converting LinkedHashsSet to ArrayList. 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 overridden the toString () method in the 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;
    }

}
+3
source share
3 answers

Do not use ArrayAdapter. Instead, from a BaseAdapter . You will have to duplicate some of the actions ArrayAdapter, but you will not be hindered by its limitations.

+3
source

BaseAdapter getCount, LinkedHashSet.

+1

Unfortunately no, you cannot. If you look at the ArrayAdapterAPI link , you will see that you can only specify an array or list of entries.

0
source

All Articles