This tutorial demonstrates how to display an array of Java enum in the Android UI. This example is also applicable if using an array or List of JavaBeans or plain Java Objects.
Core Concepts Covered
- Use the built-in android.R.layout.simple_list_item_2 to create an Android UI List.
- Use android.widget.SimpleAdapter to display an array of Java enum in the Android UI List.
Summary Technical Steps
- Create class extending android.app.ListActivity.
- Obtain data as an array of Java enum entries.
- Convert the array of enum into a List<Map<String, String>> format that will be used by the android.widget.SimpleAdapter constructor.
- Create an android.widget.SimpleAdapter that connects the List<Map<String, String>> data to android.R.layout.simple_list_item_2.
- Bind the android.widget.ListAdapter using the ListActivity.setListAdapter method.
Detailed Technical Steps
1. Create class extending android.app.ListActivity.
public class MainActivity extends ListActivity {
@Override
protected final void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
2. Obtain data as an array of Java enum entries, using the built-in enum method
values(), e.g. UsState.values() .
For this tutorial, we will use an enum containing the 13 US original colonies. View the full UsState.java code listing.
public enum UsState {
DE("Delaware"),
PA("Pennsylvania"),
NJ("New Jersey");
private String stateName;
private UsState(final String name) {
this.stateName = name;
}
public String getStateName() {
return this.stateName;
}
public String getAbbreviation() {
return this.name();
}
}
3. Convert the array of enum into a List<Map<String, String>> format that will be used by the android.widget.SimpleAdapter constructor.
private static final String TEXT1 = "text1";
private static final String TEXT2 = "text2";
private List<Map<String, String>> convertToListItems(final UsState[] states) {
final List<Map<String, String>> listItem =
new ArrayList<Map<String, String>>(states.length);
for (final UsState state: states) {
final Map<String, String> listItemMap = new HashMap<String, String>();
listItemMap.put(TEXT1, state.getStateName());
listItemMap.put(TEXT2, state.getAbbreviation());
listItem.add(Collections.unmodifiableMap(listItemMap));
}
return Collections.unmodifiableList(listItem);
}
The convertToListItems method converts an array of UsState into a List of Map. The List of Map will be used as part of the constructor for a SimpleAdapter.
Each element in the List will correspond to a row in the Android UI list. The Map<String, String> maps the attributes on the Object to the android.R.id.text1 and android.R.id.text2 UI fields.
For example, if you input
UsState[] =
[ DE("Delaware"),
PA("Pennsylvania"),
NJ("New Jersey")
];
the output will be
List<Map<String, String>> =
[ {text1="Delaware", text2="DE"},
{text1="Pennsylvania", text2="PA"},
{text1="New Jersey", text2="NJ"}
];
4. Create an android.widget.SimpleAdapter in the ListActivity class from step 1 that connects the List<Map<String, String>> data to android.R.layout.simple_list_item_2.
private ListAdapter createListAdapter(final UsState[] states) {
final String[] fromMapKey = new String[] {TEXT1, TEXT2};
final int[] toLayoutId = new int[] {android.R.id.text1, android.R.id.text2};
final List<Map<String, String>> list = convertToListItems(states);
return new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2,
fromMapKey, toLayoutId);
}
In the createListAdapter method, the constructor of the SimpleAdapter binds the keys from the Map<String, String> created in convertToListItems to the android.R.id.text1 and text2. It associates "text1" from the fromMapKey array to the android.R.id.text1 in the toLayoutId array. When the ListView needs to show text for the android.R.id.text1 ID, it executes a Map.get("text1") to get the String value to display, e.g. Delaware.
5. Bind the android.widget.ListAdapter using the ListActivity.setListAdapter method.
public class MainActivity extends ListActivity {
@Override
protected final void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final UsState[] states = UsState.values();
final ListAdapter listAdapter = createListAdapter(states);
setListAdapter(listAdapter);
}