SyntaxHighlighter JS


Android ListView Tutorial: simple_list_item_2 and SimpleAdapter

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
  1. Use the built-in android.R.layout.simple_list_item_2 to create an Android UI List.
  2. Use android.widget.SimpleAdapter to display an array of Java enum in the Android UI List.
Summary Technical Steps
  1. Create class extending
  2. Obtain data as an array of Java enum entries.
  3. Convert the array of enum into a List<Map<String, String>> format that will be used by the android.widget.SimpleAdapter constructor.
  4. Create an android.widget.SimpleAdapter that connects the List<Map<String, String>> data to android.R.layout.simple_list_item_2.
  5. Bind the android.widget.ListAdapter using the ListActivity.setListAdapter method.
Detailed Technical Steps

1. Create class extending

public class MainActivity extends ListActivity {

    protected final void onCreate(final Bundle 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 code listing.

    public enum UsState {
        NJ("New Jersey");

           private String stateName;

        private UsState(final String name) {
            this.stateName = name;

        public String getStateName() {
    return this.stateName;

        public String getAbbreviation() {


    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());

        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 and UI fields.

    For example, if you input
    UsState[] =
        [ DE("Delaware"),
          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[] {,};
        final List<Map<String, String>> list = convertToListItems(states);

        return new SimpleAdapter(this, list,
                                 fromMapKey, toLayoutId);

    In the createListAdapter method, the constructor of the SimpleAdapter binds the keys from the Map<String, String> created in convertToListItems to the and text2. It associates "text1" from the fromMapKey array to the in the toLayoutId array. When the ListView needs to show text for the 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 {

        protected final void onCreate(final Bundle savedInstanceState) {

    final UsState[] states = UsState.values();
            final ListAdapter listAdapter = createListAdapter(states);

    View the full code listing.

    The final result will look like this

    Source Code: Git | SVN | Download Zip


    1. mListAdapter = new FirebaseListAdapter(getActivity(), ChatMessage.class,
      android.R.layout.two_line_list_item, mFirebaseRef) {
      protected void populateView(View v, ChatMessage model, int position) {

      How can i change this two textview ??