我有一个自定义的枚举类:
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);
但是在侦听器本身中,我不知道如何获取该值。
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) {
}
};
怎么做到呢?
通过如下调用使其变得简单:
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Frequency_Unit freq = (Frequency_Unit) parent.getItemAtPosition(pos);
//do whatever with 'freq'
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句