Java 使用具有字符串资源的枚举填充微调器
我有一个枚举,其属性映射到字符串资源id,如下所示:Java 使用具有字符串资源的枚举填充微调器,java,android,enums,android-arrayadapter,android-spinner,Java,Android,Enums,Android Arrayadapter,Android Spinner,我有一个枚举,其属性映射到字符串资源id,如下所示: public enum MyEnum{ FIRST(1,R.string.first_enum_desc), SECOND(2,R.string.second_enum_desc); private int mId; private int mDescriptionResourceId; private MyEnum(id,descriptionResourceId) { mId = id;
public enum MyEnum{
FIRST(1,R.string.first_enum_desc),
SECOND(2,R.string.second_enum_desc);
private int mId;
private int mDescriptionResourceId;
private MyEnum(id,descriptionResourceId) {
mId = id;
mDescriptionResourceId = descriptionResourceId;
}
public toString(context){
return context.getString(mDescriptionResourceId);
}
}
我想用枚举填充微调器,问题只是使用我类型的适配器:
Spinner spinner;
spinner.setAdapter(new ArrayAdapter<MyEnum>(this, android.R.layout.simple_spinner_item, MyEnum.values()));
微调器微调器;
setAdapter(新的ArrayAdapter(这个,android.R.layout.simple_spinner_项,MyEnum.values());
我没有得到字符串资源描述,因为适配器隐式调用toString(),后者返回枚举名。我不知道该怎么办。不管我需要什么上下文来获取字符串值。有人能提出实现我所追求目标的最佳方法吗?我只需要一个指向正确方向的点。如有任何建议,将不胜感激。多谢 您应该构建自己的阵列适配器。然后重写getView()和getDropDownView方法
public class MyAdapter extends ArrayAdapter<MyEnum> {
public MyAdapter (Context context) {
super(context, 0, MyEnum.values());
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CheckedTextView text= (CheckedTextView) convertView;
if (text== null) {
text = (CheckedTextView) LayoutInflater.from(getContext()).inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
}
text.setText(getItem(position).getDescriptionResourceId());
return text;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
CheckedTextView text = (CheckedTextView) convertView;
if (text == null) {
text = (CheckedTextView) LayoutInflater.from(getContext()).inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
}
text.setText(getItem(position).getTitle());
return text;
}
}
公共类MyAdapter扩展了ArrayAdapter{
公共MyAdapter(上下文){
super(context,0,MyEnum.values());
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
CheckedTextView text=(CheckedTextView)convertView;
if(text==null){
text=(CheckedTextView)LayoutInflater.from(getContext()).flate(android.R.layout.simple\u微调器\u下拉菜单\u项,父项,false);
}
setText(getItem(position).getDescriptionResourceId());
返回文本;
}
@凌驾
公共视图getDropDownView(int位置、视图转换视图、视图组父视图){
CheckedTextView text=(CheckedTextView)convertView;
if(text==null){
text=(CheckedTextView)LayoutInflater.from(getContext()).flate(android.R.layout.simple\u微调器\u下拉菜单\u项,父项,false);
}
setText(getItem(position.getTitle());
返回文本;
}
}
检索枚举的最简单方法是获取其视图位置或项目位置:传递枚举值时,它们将按声明顺序显示。例如,在侦听器中:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MyEnum value = MyEnum.values()[position];
...
}
最后,您还可以从适配器检索枚举并强制转换它,例如:
MyEnum.class.cast(myListView.getAdapter().getItem(position))
另一种解决方案是检索视图的文本,然后使用enum.valueOf
解析枚举,但这需要知道显示该项的`TextView的id,与第一种解决方案相比似乎复杂。最后,您还可以从适配器检索枚举并强制转换它,例如:MyEnum.class.cast(myListView.getAdapter().getItem(position))
@Antonie Marques-感谢您的回复。很抱歉,我不知道这里的内容如何帮助我在微调器本身中显示字符串资源值。你能解释一下吗?也许我误解了这个问题,我以为你想从适配器获取枚举。但是使用ArrayAdapter
是很简单的,因为ListView
使用每个项目的toString
值填充视图。这不是你已经做的吗?好吧,我明白了。@bbrakenhoff提供的另一个答案是正确的:您应该覆盖与Adapter
的getView
相关的方法
MyEnum.class.cast(myListView.getAdapter().getItem(position))