android获取spinner的值,从Android Spinner获取枚举值

I've written the class below to let me get an enumerated value out of an Android Spinner.

There are two lines in getValue() neither of which compile.

How should I do this?

public class EnumSpinnerListener implements AdapterView.OnItemSelectedListener {

private String mValue = null;

public EnumSpinnerListener(AdapterView> adapterView) {

adapterView.setOnItemSelectedListener(this);

}

@Override

public void onItemSelected(AdapterView> adapterView, View view, int i, long l) {

mValue = adapterView.getItemAtPosition(i).toString();

}

@Override

public void onNothingSelected(AdapterView> adapterView) {

// do nothing

}

public T getValue() {

return Enum.valueOf(T.class, mValue); // cannot select from a type variable

return T.valueOf(mValue); // valueOf(java.lang.Class, String) in enum cannot be applied to (java.lang.String)

}

}

解决方案

Due to type erasure, T will have no meaning at runtime, which is why the expression T.class is illegal. The workaround is to reference a Class instance:

public class EnumSpinnerListener> // note the correction here

implements AdapterView.OnItemSelectedListener {

private final Class type;

private String mValue = null;

public EnumSpinnerListener(Class type, AdapterView> adapterView) {

this.type = type;

adapterView.setOnItemSelectedListener(this);

}

public T getValue() {

return Enum.valueOf(type, mValue);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值