Java 从Android中的ArrayAdapter访问资源
我定义了一些资源,例如:Java 从Android中的ArrayAdapter访问资源,java,android,android-arrayadapter,Java,Android,Android Arrayadapter,我定义了一些资源,例如: <color name="lightGrey">#FFCCCCCC</color> <integer name="KEY_POSITION_ARM">2</integer> …但我会遇到类似“类型ContinuityAdapter的方法getResources()未定义”的错误。(连续自适应扩展阵列自适应) 有什么好办法吗 谢谢 这是一个例子: switch (currentModule.keyPosition) { c
<color name="lightGrey">#FFCCCCCC</color>
<integer name="KEY_POSITION_ARM">2</integer>
…但我会遇到类似“类型ContinuityAdapter的方法getResources()未定义”的错误。(连续自适应扩展阵列自适应)
有什么好办法吗
谢谢
这是一个例子:
switch (currentModule.keyPosition) {
case activity.getResources().getInteger(R.integer.KEY_POSITION_ARM):
moduleDataView.keyPosition.setText("TEST");
moduleDataView.keyPosition.setTextColor(Color.GREEN);
break;
case R.integer.KEY_POSITION_ARM:
moduleDataView.keyPosition.setText("ARM");
moduleDataView.keyPosition.setTextColor(Color.RED);
break;
}
第一种情况给出一个错误,第二种情况也不使用XML文件中的值。虽然正如你所说,我可以用R。。。只要我在任何地方都这样使用它,它就是有价值的。只是不确定这是否被视为“最佳实践”。谢谢您需要一个对象来调用方法。通常,您可以通过自定义适配器的构造函数传递上下文或其子类(即活动)
比如:
然后使用:
mContext.getResources()...
编辑:
这似乎是避免切换的情况。见:
public ContinuityAdapter extends ArrayAdapter {
private final Context mContext;
...
public ContinuityAdapter(Context context) {
mContext = context;
}
}
mContext.getResources()...