Creates a standard Android spinner adapter. - Android User Interface

Android examples for User Interface:Spinner

Description

Creates a standard Android spinner adapter.

Demo Code


//package com.java2s;

import java.util.Arrays;
import java.util.List;
import android.content.Context;

import android.widget.ArrayAdapter;

public class Main {
    /**/*from  w  w  w .j  a  v  a  2  s  .co  m*/
     * Creates a standard Android spinner adapter.  Input items can be of any type.
     * 
     * @param context Context with which to create the adapter
     * @param items List of items to populate the adapter with
     * @return The created adapter
     */
    public static <E> ArrayAdapter<E> createSpinnerAdapter(Context context,
            List<E> items) {
        ArrayAdapter<E> adapter = createAdapter(context,
                android.R.layout.select_dialog_item, items);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        return adapter;
    }

    /**
     * Creates a standard Android spinner adapter.  Input items can be of any type.
     * 
     * @param context Context with which to create the adapter
     * @param items Array of items to populate the adapter with
     * @return The created adapter
     */
    public static <E> ArrayAdapter<E> createSpinnerAdapter(Context context,
            E[] items) {
        return createSpinnerAdapter(context, Arrays.asList(items));
    }

    /**
     * Creates a standard Android adapter.  Input items can be of any type.
     * 
     * @param context Context with which to create the adapter
     * @param layoutId Android resource id to be used for a list row
     * @param items List of items to populate the adapter with
     * @return The created adapter
     */
    public static <E> ArrayAdapter<E> createAdapter(Context context,
            int layoutId, List<E> items) {
        return new ArrayAdapter<E>(context, layoutId, items);
    }
}

Related Tutorials