如何使用枚举的吸气剂?

时间:2019-07-01 11:37:57

标签: java android object enums model

我有一个称为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(),以便设置与我的ExpenseType相对应的图标?

我在适配器中有以下代码:

private void checkSelect(ExpenseType expenseType) {
            if(positionSelected == getAdapterPosition()){
                gridIcon.setImageResource(????????);
                return;
            }

如何访问该吸气剂而不是????????这样我就可以将我的可绘制值存储在该枚举中?

2 个答案:

答案 0 :(得分:0)

expenseType似乎是类类型ExpenseType,但是您要引用的枚举是内部枚举type(应重命名为btw)。因此,您需要在type中为ExpenseType提供一个字段,并为此提供一个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
}

答案 1 :(得分:0)

这似乎是您班级设计中的一个小问题。

您的枚举PROCEDURE PROCESS_ASSETS AS BEGIN INSERT ALL -- 1 WHEN OP_TYPE = 'I' THEN INTO XXCCS_UEE_SRVC_ES_ACT_KEY ( COVERED_LINE_ID, INSTANCE_ID, SEQ_ID, OP_TYPE, SOT_TABLE, SOT_CREATE_DATE, SOT_LAST_UPDATE_DATE, GG_CREATE_DATE, CONTRACT_ID, ITEM_TYPE_FLAG ) VALUES ( COVERED_LINE_ID, INSTANCE_ID, SEQ_ID, OP_TYPE, 'L_SOT_TABLE_NAME', NRT_CREATION_DATE, NRT_LAST_UPDATE_DATE, GG_CREATE_DATE, CONTRACT_ID, ITEM_TYPE_FLAG ) --2 WHEN OP_TYPE = 'I' THEN INTO XXCCS_UEE_DEVC_ES_CVD_KEY ( INSTANCE_ID, OP_TYPE, SOT_TABLE, SOT_CREATE_DATE, SOT_LAST_UPDATE_DATE, GG_CREATE_DATE, SEQ_ID, ITEM_TYPE_FLAG ) VALUES ( INSTANCE_ID, OP_TYPE, 'L_SOT_TABLE_NAME', NRT_CREATION_DATE, NRT_LAST_UPDATE_DATE, GG_CREATE_DATE, SEQ_ID, ITEM_TYPE_FLAG ) --3 WHEN OP_TYPE = 'U' THEN INTO XXCCS_UEE_SRVC_ES_ACT_KEY ( COVERED_LINE_ID, INSTANCE_ID, SEQ_ID, OP_TYPE, SOT_TABLE, SOT_CREATE_DATE, SOT_LAST_UPDATE_DATE, GG_CREATE_DATE, CONTRACT_ID, ITEM_TYPE_FLAG ) VALUES ( COVERED_LINE_ID, INSTANCE_ID, SEQ_ID, OP_TYPE, 'L_SOT_TABLE_NAME', NRT_CREATION_DATE, NRT_LAST_UPDATE_DATE, GG_CREATE_DATE, CONTRACT_ID, ITEM_TYPE_FLAG ) -- 4 WHEN OP_TYPE = 'U' THEN INTO XXCCS_UEE_DEVC_ES_CVD_KEY ( INSTANCE_ID, OP_TYPE, SOT_TABLE, SOT_CREATE_DATE, SOT_LAST_UPDATE_DATE, GG_CREATE_DATE, SEQ_ID, ITEM_TYPE_FLAG ) VALUES ( INSTANCE_ID, OP_TYPE, 'L_SOT_TABLE_NAME', NRT_CREATION_DATE, NRT_LAST_UPDATE_DATE, GG_CREATE_DATE, SEQ_ID, ITEM_TYPE_FLAG ) -- 5 WHEN OP_TYPE = 'U' AND ( ( ITEM_TYPE_FLAG IN ( 'P', 'S' ) AND OLD_ITEM_TYPE_FLAG IN ( 'C' ) ) OR ( ITEM_TYPE_FLAG IN ( 'C' ) AND OLD_ITEM_TYPE_FLAG IN ( 'P', 'S' ) ) ) THEN INTO XXCCS_UEE_SRVC_ES_ACT_KEY ( COVERED_LINE_ID, INSTANCE_ID, SEQ_ID, OP_TYPE, SOT_TABLE, SOT_CREATE_DATE, SOT_LAST_UPDATE_DATE, GG_CREATE_DATE, CONTRACT_ID, ITEM_TYPE_FLAG ) VALUES ( COVERED_LINE_ID, INSTANCE_ID, SEQ_ID, 'D', 'L_SOT_TABLE_NAME', NRT_CREATION_DATE, NRT_LAST_UPDATE_DATE, GG_CREATE_DATE, CONTRACT_ID, OLD_ITEM_TYPE_FLAG ) -- 6 WHEN OP_TYPE = 'U' AND ( ( ITEM_TYPE_FLAG IN ( 'P', 'S' ) AND OLD_ITEM_TYPE_FLAG IN ( 'C' ) ) OR ( ITEM_TYPE_FLAG IN ( 'C' ) AND OLD_ITEM_TYPE_FLAG IN ( 'P', 'S' ) ) ) THEN INTO XXCCS_UEE_DEVC_ES_CVD_KEY ( INSTANCE_ID, OP_TYPE, SOT_TABLE, SOT_CREATE_DATE, SOT_LAST_UPDATE_DATE, GG_CREATE_DATE, SEQ_ID, ITEM_TYPE_FLAG ) VALUES ( INSTANCE_ID, 'D', 'L_SOT_TABLE_NAME', NRT_CREATION_DATE, NRT_LAST_UPDATE_DATE, GG_CREATE_DATE, SEQ_ID, OLD_ITEM_TYPE_FLAG ) -- 7 ELSE INTO XXCCS_UEE_SRVC_ES_ACT_KEY ( COVERED_LINE_ID, INSTANCE_ID, SEQ_ID, OP_TYPE, SOT_TABLE, SOT_CREATE_DATE, SOT_LAST_UPDATE_DATE, GG_CREATE_DATE, CONTRACT_ID, ITEM_TYPE_FLAG ) VALUES ( COVERED_LINE_ID, INSTANCE_ID, SEQ_ID, OP_TYPE, 'L_SOT_TABLE_NAME', NRT_CREATION_DATE, NRT_LAST_UPDATE_DATE, GG_CREATE_DATE, CONTRACT_ID, ITEM_TYPE_FLAG ) SELECT ROWID, COVERED_LINE_ID, OP_TYPE, SEQ_ID, NRT_CREATION_DATE, NRT_LAST_UPDATE_DATE, CONTRACT_ID, SERVICE_LINE_ID, GG_CREATE_DATE, INSTANCE_ID, ITEM_TYPE_FLAG, NVL(OLD_ITEM_TYPE_FLAG, ITEM_TYPE_FLAG) OLD_ITEM_TYPE_FLAG FROM XXCCS_DS_CVDLIB_DTLEB_ES; END PROCESS_ASSETS; / type类中的嵌套类。 (注意:因为它是一个枚举,所以它隐式为ExpenseType嵌套类型)。

为了使您能够调用static的访问器,您将需要以某种方式引用其特定类型。

一种方法是将type作为type的实例字段,就像ExpenseType一样。

然后,您需要将特定的String gridText类型绑定到您的type实例(我知道,这在语义上会造成混淆,但是我没有给您的变量命名:)。

换句话说,ExpenseType的每个实例都有自己的ExpenseType字段,该字段分配了一种类型的... errr .. type

因此type的实例1的ExpenseType的值为FOODtype的实例2的{{1}的值为ExpenseType }等。

然后您可以通过调用添加吸气剂并引用FLOWERS中的type

drawableBlue int