Passing enum or object through an intent (the best solution)

前端 未结 15 995
时光取名叫无心
时光取名叫无心 2020-12-04 05:41

I have an activity that when started needs access to two different ArrayLists. Both Lists are different Objects I have created myself.

Basically I need a way to pa

15条回答
  •  感情败类
    2020-12-04 06:11

    you can use enum constructor for enum to have primitive data type..

    public enum DaysOfWeek {
        MONDAY(1),
        TUESDAY(2),
        WEDNESDAY(3),
        THURSDAY(4),
        FRIDAY(5),
        SATURDAY(6),
        SUNDAY(7);
    
        private int value;
        private DaysOfWeek(int value) {
            this.value = value;
        }
    
        public int getValue() {
            return this.value;
        }
    
        private static final SparseArray map = new SparseArray();
    
        static
        {
             for (DaysOfWeek daysOfWeek : DaysOfWeek.values())
                  map.put(daysOfWeek.value, daysOfWeek);
        }
    
        public static DaysOfWeek from(int value) {
            return map.get(value);
        }
    }
    

    you can use to pass int as extras then pull it from enum using its value.

提交回复
热议问题