我有一个自定义枚举类:
public static enum Frequency_Unit{
MINUTES("Minutes", 0),
HOURS("Hours", 1),
DAYS("Days", 2),
WEEKS("Weeks", 3),
MONTHS("Months", 4),
YEARS("Years", 5);
private String stringValue;
private int intValue;
private Frequency_Unit(String toString, int value) {
this.stringValue = toString;
this.intValue = value;
}
@Override
public String toString() {
return stringValue;
}
}
我用它来设置微调器:
spinner_frequency = (Spinner) findViewById(R.id.spinner_repeat_frequency);
spinner_frequency.setAdapter(new ArrayAdapter<Constants.Frequency_Unit>(this,
android.R.layout.simple_spinner_item, Constants.Frequency_Unit.values()));
现在我想用 onItemSelectedListener 检索数值(即,如果用户选择“Hours”,则为 1):
spinner_frequency.setOnItemSelectedListener(spinner_frequency_listener);
但是在侦听器本身中,我不知道如何检索该值。
OnItemSelectedListener spinner_frequency_listener = new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
if (parent.getChildCount() > 0){
int repeat_frequency = //**VALUE SHOULD BE HERE**
}
}
// Default line, when nothing is selected
public void onNothingSelected(AdapterView<?> arg0) {
}
};
怎么做到呢?