Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Enums to Array Adapter for Spinner in Android

I'm looking to use enums for a list inside of a spinner widget on android. I have my enums setup as follows:

public enum States{

AL("Alabama"), 
AK("Alaska"), 
AR("Arkansas"), 
AZ("Arizona"), 
CA("California"), 
CO("Colorado"),
    ... (etc.)
}

My current array adapter is setup as follows:

mAddressState.setAdapter(new ArrayAdapter<States>(this, android.R.layout.simple_list_item_1, States.values()));

This almost works, but in my spinner list I end up with the abbreviations, rather than the state names (which is what I'm going for). Is there a workaround to get this setup correctly?

like image 229
RyanInBinary Avatar asked Aug 24 '11 22:08

RyanInBinary


3 Answers

I hope that this helps someone. It took me a while to figure it out for myself. The trick is to override toString in your enum. The code for your states enum would be:

public enum States{
  AL("Alabama"), 
  AK("Alaska"), 
  AR("Arkansas"), 
  AZ("Arizona"), 
  CA("California"), 
  CO("Colorado"),
    ... (etc.);

  private String theState;

  States(String aState) {
    theState = aState;
  }

  @Override public String toString() {
    return theState;
  }
}

Then, create the adapter just as you do:

   mAddressState.setAdapter(new ArrayAdapter<States>(this,
      android.R.layout.simple_list_item_1, States.values()));

and the long names will show up in the spinner. To get the abbreviated name from the adapter, to store the selected one for instance, use the enum.name() function. For instance:

Spinner spinner = (Spinner) myView.findViewById(R.id.theState);
String selected = ((States)spinner.getSelectedItem()).name();
like image 160
LocalTrav Avatar answered Nov 06 '22 08:11

LocalTrav


Let's try to provide the answer with the help of an example

public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } 

Consider the above enum. Now you can add it to an adapter in the following ways:

Method 1:

ArrayAdapter<Day> adapter = new ArrayAdapter<Day>(activity, android.R.layout.simple_spinner_dropdown_item, Day.values());

Note: You can override toString method in enum definition to display required values incase of advanced enums.

Method 2:

public static final List<String> daysList = Stream.of(Day.values()).map(Day::name).collect(Collectors.toList());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_dropdown_item, daysList);
like image 33
Rajeet Goyal Avatar answered Nov 06 '22 08:11

Rajeet Goyal


Here is how to do it using Kotlin and Material TextInputLayout which can be used as Spinner replacement, first implement it like described in this answer: https://stackoverflow.com/a/57746352/2472350

first define the enum like this:

enum class States(val label: String) {
    AL("Alabama"), 
    AK("Alaska"), 
    AR("Arkansas"), 
    AZ("Arizona"), 
    CA("California"), 
    CO("Colorado"),;

    override fun toString(): String {
        return label
    }
}

then set an adapter like this:

val items = OrderStatusType.values()
val adapter = ArrayAdapter(requireContext(), R.layout.spinner_item_tv, items)
autoCompleteTextView.setAdapter(adapter)

the spinner_item_tv could be any TextView, like this:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/spinner_item_tv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:text="@tools:sample/cities" />

to get the selected item:

val selectedState = autoCompleteTextView.text.toString()

you can also find the enum value by searching through them like this:

val selectedState = States.values().firstOrNull { it.label == autoCompleteTextView.text.toString() } 
// could be null if nothing is selected
like image 1
Amin Keshavarzian Avatar answered Nov 06 '22 08:11

Amin Keshavarzian