Updated: Key Value Pair Spinner (Android)

I did a little tweaking and optimizing for my key value spinner. I looked at the android source and realized that the BaseAdapter already implements the SpinnerAdapter. Infact I could simply extend the ArrayAdapter and let it do all the liftin for me.

I also wanted the drop down to look like the simple drop down but I wanted the list to look like the radio button list. A little digging through the Android source code and I found what I needed. Here is a stripped down version of my code.

public class KeyValueAdapter
    extends ArrayAdapter<String>
{
    private final Hashtable<String, CameraSource> _data;
    private final String[] _keys;

    public KeyValueAdapter(Context context, Hashtable<String, CameraSource> objects)
    {
        //To make the drop down a simple text box
        super(context, android.R.layout.simple_spinner_item);
        _data = objects;

        //To make the drop down view a radio button list
        setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        
        // TODO; Do your own data massaging here to present the data in a ordered list.
        
        //Assign hash keys with a position so that we can present and retrieve them
        int i = 0;
        _keys = new String[_data.size()];

        for (final Enumeration<String> e = _data.keys(); e.hasMoreElements();)
        {
            _keys[i++] = e.nextElement().toString();
        }
    }


    public int getCount()
    {
        return _data.size();
    }


    /**
     * Returns the camera name for the camera in the specified position 
     */
    @Override
    public String getItem(int position)
    {
        //TODO: return the value based on the position. This is displayed in the list.
    }

    /**
     * Returns the cameraKey for the camera in the specified position 
     */
    public long getItemId(int position)
    {
    //TODO: Return an id to represent the item. 
    
    return position;    // if this function is of no use to you simply return the position. 
                        // if your identifier is a different return type then you can make 
                        // your own method  eg: public String getPromaryKey(int position) and 
                        // call it where you need your identifier.
    }
}

0 comments: