0

我有一个自定义枚举类:

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) {    

    }
};

怎么做到呢?

4

2 回答 2

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'
}
于 2013-11-14T13:03:49.893 回答
1
@Override
            public void onItemSelected(AdapterView<?> parent,
                View arg1, int position, long arg3) {
                Beantype genreBean = (Beantype ) parent
                    .getItemAtPosition(position);
}
于 2013-11-14T13:02:02.370 回答