简体   繁体   中英

How to use the getter of an enum?

I have this model of an object called 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. 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?

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). 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).

As for the renaming: the class ExpenseType has a field gridText which might indicate that it actually represents a cell. 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:

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. (Note: as it's an enum, it's implicitly a static nested type).

In order for you to be able to invoke the accessors of a type , you will need to reference its specific type somehow.

One way to do so would be to have a type as instance field of ExpenseType , pretty much like the 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 :).

In other words, each instance of ExpenseType has its own type field assigned with one type of... 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.

You could then add a getter and reference the drawableBlue int in checkSelect by invoking:

// assuming field type can't be null
expenseType.getType().getDrawableBlue()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM