Code example for Spinner

Methods: setAdapter, setOnItemSelectedListener, setSelection

0
 
		lbl = new TextView(context);
		lbl.setText("Field: ");
		addView(lbl, new LinearLayout.LayoutParams(WIDTH, LayoutParams.WRAP_CONTENT, 1));
 
		spin = new Spinner(context);
		addView(spin, new LinearLayout.LayoutParams(WIDTH, LayoutParams.WRAP_CONTENT, 1));
 
		// Update the value when the edit box changes 
		spin.setOnItemSelectedListener(new OnItemSelectedListener() {
 
			@Override 
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				value = spin.getSelectedItemPosition();
				if (changeListener != null && localUpdate == false)
					changeListener.run();
			} 
 
			@Override 
			public void onNothingSelected(AdapterView<?> arg0) {
			} 
		}); 
 
		setPadding(5,5,5,5);
 
		setMinimumWidth(300);
		setValue(0); 
	} 
 
	//! This is the constructor used by the code 
	public EnumFieldView(Context context, AttributeSet attrs, UAVObjectField field, int idx) {
		this(context, attrs);
 
		// Set the label name 
		String name = field.getName();
		List<String> elements = field.getElementNames();
		if (elements != null && elements.size() > 1) {
			name = name + "-" + elements.get(idx);
		} 
		lbl.setText(name);
 
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		adapter.addAll(field.getOptions());
		spin.setAdapter(adapter);
		spin.setSelection((int) field.getDouble(idx));
	} 
 
	@Override 
	public double getValue() { 
		return value;
	} 
 
	@Override 
	public void setValue(double val) {
		localUpdate = true;
		Log.d(TAG, "Value set to: " + val);
		value = val;
		spin.setSelection((int) val);
		localUpdate = false;
	} 
 
	@Override 
	public void setOnChangedListener(Runnable run) {