Code example for Spinner

Methods: setAdapter, setOnItemSelectedListener

0
        activity.startManagingCursor(cursor);
        final SimpleCursorAdapter ad = new SimpleCursorAdapter(activity, R.layout.simple_spinner_item, cursor, from, to);
        ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 
        final Spinner spinner = (Spinner)activity.findViewById(id);
        spinner.setAdapter(ad);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(final AdapterView<?> adapterView, final View view, final int i, final long l) {
                final Cursor c = (Cursor) spinner.getSelectedItem();
                if (c != null) {
                    final int index = c.getColumnIndex(value);
                    if (index >= 0) listener.select(c.getString(index));
                } 
            } 
 
            public void onNothingSelected(final AdapterView<?> adapterView) {
                listener.none();
            } 
        }); 
 
        return spinner;