[英]How to use the getter of an enum?
I have this model of an object called expenseType: 我有一个称为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;
}
}
The string gridText gets written inside a database, but I do not want to add the drawable values to the database as well, so I created that enum that has the drawable variation. 字符串gridText被写入数据库内部,但是我也不想将可绘制的值添加到数据库中,因此我创建了具有可绘制变化的枚举。 Now, inside a recycle view adapter, how can I access the getDrawableBlue() from the enum so I can set an icon correspondent to my expenseType?
现在,在回收视图适配器中,如何从枚举中访问getDrawableBlue(),以便可以设置与我的costumType对应的图标?
I have this code in the adapter: 我在适配器中有以下代码:
private void checkSelect(ExpenseType expenseType) {
if(positionSelected == getAdapterPosition()){
gridIcon.setImageResource(????????);
return;
}
How can I access that getter instead of the ???????? 如何访问该吸气剂而不是???????? so I can get my drawable value stored in that enum?
这样我就可以将我的可绘制值存储在该枚举中?
expenseType
seems to be of the class type ExpenseType
but the enum you're refering to is the inner enum type
(which should be renamed btw). expenseType
似乎是ExpenseType
类类型,但是您要引用的枚举是内部枚举type
(应将其重命名为btw)。 Thus you'd need to either provide a field for type
in ExpenseType
and a getter for that and then call something like expenseType.getType().getDrawableBlue()
or refactor your code (depends on what ExpenseType
is meant to represent). 因此,您需要为
ExpenseType
type
提供一个字段,并为此提供一个getter,然后调用诸如expenseType.getType().getDrawableBlue()
的代码或重构您的代码(取决于ExpenseType
所代表的含义)。
As for the renaming: the class ExpenseType
has a field gridText
which might indicate that it actually represents a cell. 至于重命名:
ExpenseType
类具有一个gridText
字段,该字段可能指示它实际上表示一个单元格。 If that's the case I'd suggest doing something like this: 如果是这种情况,我建议您这样做:
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),
...;
}
If gridText
isn't a cell's text but the same for every type then you might want to merge the inner enum type
into ExpenseType
, ie something like this: 如果
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
}
This looks like a glitch in your class design. 这看起来像是您班级设计中的一个小故障。
Your enum type
is a nested class in your ExpenseType
class. 您的枚举
type
是ExpenseType
类中的嵌套类。 (Note: as it's an enum, it's implicitly a static
nested type). (注意:因为它是一个枚举,所以它隐式地是一个
static
嵌套类型)。
In order for you to be able to invoke the accessors of a type
, you will need to reference its specific type somehow. 为了使您能够调用
type
,您将需要以某种方式引用其特定类型。
One way to do so would be to have a type
as instance field of ExpenseType
, pretty much like the String gridText
. 一种方法是将
type
为ExpenseType
实例字段,非常类似于String gridText
。
You would then need to bind a specific type
type to your ExpenseType
instance (I know, this gets semantically confusing but I didn't name your variables :). 然后,您需要将特定
type
类型绑定到您的ExpenseType
实例(我知道,这在语义上会造成混淆,但是我没有给您的变量命名:)。
In other words, each instance of ExpenseType
has its own type
field assigned with one type of... errr.. type
. 换句话说,每个
ExpenseType
实例都有其自己的type
字段,并为其分配了一种... errr .. type
。
So instance 1 of ExpenseType
has a FOOD
value for its type
, instance 2 of ExpenseType
has a FLOWERS
value for its type
, etc. 这样的实例1
ExpenseType
具有FOOD
其值type
,实例2 ExpenseType
具有FLOWERS
其值type
等
You could then add a getter and reference the drawableBlue int
in checkSelect
by invoking: 然后,您可以通过调用以下方法添加一个getter并在
checkSelect
引用drawableBlue int
:
// assuming field type can't be null
expenseType.getType().getDrawableBlue()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.