728x90

이번에는 MainActivity.java 에서

ArrayList<HashMap<String, String>> calList = new ArrayList<HashMap<String, String>>();

LinkedHashMap<String, Calendar_Item> calList = new LinkedHashMap<String, Calendar_Item>();

로 변경하면 어떤 것들을 수정해줘야 할까?


먼저 https://link2me.tistory.com/1717 게시글을 읽고 나서 아래 비교 내용을 보면 좀 더 이해하는데 도움이 될 것으로 본다.

HashMap 을 사용하면 입력한 순서대로 출력이 될 것을 기대하지만 뒤죽박죽으로 결과를 보여준다.


MainActivity.java 수정사항

                if(CurrentMonth-1 == thisMonth){ // 현재월이면
                    if(i == thisDay){
                        Log.e(TAG, "key := " + key);
                        int index = getIndexOfCalList(key);
                        HashMap<String, String> item = calendarItem(String.valueOf(yy),String.valueOf(CurrentMonth),String.valueOf(i),weekday,"CYAN","",key);
                        calList.set(index,item);
                    }
                }


    private HashMap<String, String> calendarItem(String year, String month, String day, int weekday, String color, String name, String key){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("year",year);
        map.put("month",month);
        map.put("day",day);
        map.put("weekday", String.valueOf(weekday));
        map.put("color",color);
        map.put("event",name);
        map.put("key",key);
        return map;
    }

    private void addCalendarItem(String year, String month, String day, int weekday, String color,String name, String key){
        HashMap<String, String> item = calendarItem(year,month,day,weekday,color,name,key);
        calList.add(item);
    }

    private int getIndexOfCalList(String search_key) {
        for (int temp = 0; temp < calList.size(); temp++) {
            String key = calList.get(temp).get("key");
            if (key != null && key.equals(search_key)) {
                return temp;
            }
        }
        return -1;
    }

                if(CurrentMonth-1 == thisMonth){ // 현재월이면
                    if(i == thisDay){
                        Log.e(TAG, "key := " + key);
                        addCalendarItem(String.valueOf(yy),String.valueOf(CurrentMonth),String.valueOf(i),weekday,"CYAN","",key);
                    }
                }

    private Calendar_Item calendarItem(String year, String month, String day, int weekday, String color, String name, String key){
        Calendar_Item item = new Calendar_Item();
        item.setYear(year);
        item.setMonth(month);
        item.setDay(day);
        item.setWeekday(weekday);
        item.setColor(color);
        item.setEvent(name);
        return item;
    }

    private void addCalendarItem(String year, String month, String day, int weekday, String color,String name, String key){
        Calendar_Item item = calendarItem(year,month,day,weekday,color,name,key);
        calList.put(key,item);
    }


GridCellAdapter.java 수정사항

    private final ArrayList<HashMap<String, String>> calList;

    public GridCellAdapter(Context context, ArrayList<HashMap<String, String>> list) {
        mContext = context;
        calList = list;
    }

    public HashMap<String, String> getItem(int position) {
        return calList.get(position);
    }

        String theday = calList.get(position).get("day");
        int themonth = Integer.parseInt(calList.get(position).get("month"));
        String theyear = calList.get(position).get("year");
        String holiday = calList.get(position).get("event");

    private final LinkedHashMap<String, Calendar_Item> calList;
    private String[] mKeys;

    public GridCellAdapter(Context context, LinkedHashMap<String, Calendar_Item> list) {
        mContext = context;
        calList = list;
        mKeys = calList.keySet().toArray(new String[list.size()]);
    }

    public Calendar_Item getItem(int position) {
        return calList.get(mKeys[position]);
    }

        String theday = calList.get(mKeys[position]).getDay();
        int themonth = Integer.parseInt(calList.get(mKeys[position]).getMonth());
        String theyear = calList.get(mKeys[position]).getYear();
        String holiday = calList.get(mKeys[position]).getEvent();


어떤 것을 사용하든지 검색 속도가 느리지 않으면서 확장성을 고려한 메소드를 적절하게 사용하면 된다.


공휴일 등록, 기념일 등록, 일정 등록 등의 루틴은 여기에는 적지 않았다.


도움이 되셨다면 ... 해 주세요. 좋은 글 작성에 큰 힘이 됩니다.

블로그 이미지

Link2Me

,