Getting an enumerated value out of an Android Spinner

白昼怎懂夜的黑 提交于 2019-12-06 09:32:21
Paul Bellora

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<T> instance:

public class EnumSpinnerListener<T extends Enum<T>> // note the correction here
implements AdapterView.OnItemSelectedListener {

    private final Class<T> type;

    private String mValue = null;

    public EnumSpinnerListener(Class<T> type, AdapterView<?> adapterView) {
        this.type = type;
        adapterView.setOnItemSelectedListener(this);
    }

    public T getValue() {
        return Enum.valueOf(type, mValue);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!