Java – Causes the ArrayAdapterItemCategory to accept LinkedHashSet instead of ArrayList

Causes the ArrayAdapter to accept LinkedHashSet instead of ArrayList… here is a solution to the problem.

Causes the ArrayAdapter to accept LinkedHashSet instead of ArrayList

In my application, I use LinkhedHashSet instead of ArrayList to hold ItemCategory objects (POJOs with name and id attributes)
Now I want to load this LinkedHashSet in 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);
}

By the way, 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;
    }

}

Solution

Do not use the ArrayAdapter. Instead, start with BaseAdapter extension. You will have to replicate some of the behavior of the ArrayAdapter, but you will not be hindered by its limitations.

Related Problems and Solutions