public class SpinnerData { private String value = ""; private String text = ""; public SpinnerData() { value = ""; text = ""; } public SpinnerData(String _value, String _text) { value = _value; text = _text; } @Override public String toString() { return text; } public String getValue() { return value; } public String getText() { return text; } }
設定Spinner adapter
List<SpinnerData> list = new ArrayList<SpinnerData>(); SpinnerData sd = new SpinnerData(value, text); list.add(sd); ... ... ... ArrayAdapter<SpinnerData> adapter = new ArrayAdapter<SpinnerData>(context, android.R.layout.simple_spinner_item, list); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp.setAdapter(adapter);
get Spinner value/text
String value = ((SpinnerData) spinner.getSelectedItem()).getValue(); String text = ((SpinnerData) spinner.getSelectedItem()).getText();
沒有留言:
張貼留言