我有一个自定义枚举类:
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检索数值(例如,如果用户选择“小时”,则为1):
spinner_frequency.setOnItemSelectedListener(spinner_frequency_listener);
但是在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) {
}
};
怎么做?
答案 0 :(得分:2)
通过以下方式致电:
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Frequency_Unit freq = (Frequency_Unit) parent.getItemAtPosition(pos);
//do whatever with 'freq'
}
答案 1 :(得分:1)
@Override
public void onItemSelected(AdapterView<?> parent,
View arg1, int position, long arg3) {
Beantype genreBean = (Beantype ) parent
.getItemAtPosition(position);
}