[英]How to use the getter of an enum?
我有一个称为expenseType的对象模型:
public class ExpenseType {
private String gridText;
public enum type {
FOOD(Constants.EXPENSE_TYPE_FOOD, R.drawable.food_blue, R.drawable.food),
FLOWERS(Constants.EXPENSE_TYPE_FLOWERS, R.drawable.flowers_blue, R.drawable.flowers),
GROCERIES(Constants.EXPENSE_TYPE_GROCERIES, R.drawable.groceries_blue, R.drawable.groceries),
HOLIDAY(Constants.EXPENSE_TYPE_HOLIDAY, R.drawable.holiday_blue, R.drawable.holiday),
PHARMACY(Constants.EXPENSE_TYPE_PHARMACY, R.drawable.pharmacy_blue, R.drawable.pharmacy),
BILLS(Constants.EXPENSE_TYPE_BILLS, R.drawable.bills_blue, R.drawable.bills),
CLOTHES(Constants.EXPENSE_TYPE_CLOTHES, R.drawable.clothes_blue, R.drawable.clothes),
TRANSPORT(Constants.EXPENSE_TYPE_TRANSPORT, R.drawable.transport_blue, R.drawable.transport),
ITEMS(Constants.EXPENSE_TYPE_ITEMS, R.drawable.items_blue, R.drawable.items),
OTHERS(Constants.EXPENSE_TYPE_OTHERS, R.drawable.others_blue, R.drawable.others);
private String expenseKey;
private int drawableBlue, drawableWhite;
type(String expenseKey, @DrawableRes int drawableBlue, @DrawableRes int drawableWhite) {
this.expenseKey = expenseKey;
this.drawableBlue = drawableBlue;
this.drawableWhite = drawableWhite;
}
public String getKey() {
return expenseKey;
}
public int getDrawableBlue() {
return drawableBlue;
}
public int getDrawableWhite() {
return drawableWhite;
}
}
public ExpenseType(String gridText) {
this.gridText = gridText;
}
public String getGridText() {
return gridText;
}
public void setGridText(String gridText) {
this.gridText = gridText;
}
}
字符串gridText被写入数据库内部,但是我也不想将可绘制的值添加到数据库中,因此我创建了具有可绘制变化的枚举。 现在,在回收视图适配器中,如何从枚举中访问getDrawableBlue(),以便可以设置与我的costumType对应的图标?
我在适配器中有以下代码:
private void checkSelect(ExpenseType expenseType) {
if(positionSelected == getAdapterPosition()){
gridIcon.setImageResource(????????);
return;
}
如何访问该吸气剂而不是???????? 这样我就可以将我的可绘制值存储在该枚举中?
expenseType
似乎是ExpenseType
类类型,但是您要引用的枚举是内部枚举type
(应将其重命名为btw)。 因此,您需要为ExpenseType
type
提供一个字段,并为此提供一个getter,然后调用诸如expenseType.getType().getDrawableBlue()
的代码或重构您的代码(取决于ExpenseType
所代表的含义)。
至于重命名: ExpenseType
类具有一个gridText
字段,该字段可能指示它实际上表示一个单元格。 如果是这种情况,我建议您这样做:
public class ExpenseGridCell {
private String gridText;
private ExpenseType type; //that would be your enum tyoe
public ExpenseType getType() {
return type;
}
}
//I'll move the enum to a separate class which makes it easier to use elsewhere (the outer class would be a "namespace" only anyway)
public enum ExpenseType {
FOOD(Constants.EXPENSE_TYPE_FOOD,R.drawable.food_blue, R.drawable.food),
...;
}
如果gridText
不是单元格的文本,但每种类型都相同,那么您可能希望将内部枚举type
合并到ExpenseType
,即像这样:
public enum ExpenseType {
FOOD(Constants.EXPENSE_TYPE_FOOD, "Some grid text for food", R.drawable.food_blue, R.drawable.food),
...;
private String gridText;
private String expenseKey;
private int drawableBlue, drawableWhite;
//appropriate constructor and getters
}
这看起来像是您班级设计中的一个小故障。
您的枚举type
是ExpenseType
类中的嵌套类。 (注意:因为它是一个枚举,所以它隐式地是一个static
嵌套类型)。
为了使您能够调用type
,您将需要以某种方式引用其特定类型。
一种方法是将type
为ExpenseType
实例字段,非常类似于String gridText
。
然后,您需要将特定type
类型绑定到您的ExpenseType
实例(我知道,这在语义上会造成混淆,但是我没有给您的变量命名:)。
换句话说,每个ExpenseType
实例都有其自己的type
字段,并为其分配了一种... errr .. type
。
这样的实例1 ExpenseType
具有FOOD
其值type
,实例2 ExpenseType
具有FLOWERS
其值type
等
然后,您可以通过调用以下方法添加一个getter并在checkSelect
引用drawableBlue int
:
// assuming field type can't be null
expenseType.getType().getDrawableBlue()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.