[android] material-calendar를 이용한 Listview 일정 추가


material-calendar 라이브러리를 사용해서 캘린더를 만들고 있습니다.

이미지

Activity가 아닌 Fragment로 작성 중이고 날짜를 선택하고 밑의 버튼을 누르게 되면

이미지

다음과 같은 alertDialog를 생성하고 추가할 일정과 시간을 빈공간에 있는 ListView에 띄우려고 합니다.

날짜에 맞는 일정을 띄우려고 하는데 adapter생성을 어떻게 해야 할지 몰라 자문을 구하려 합니다.

밑은 제가 작성한 코드입니다.


public class tab2 extends Fragment{

    private static MaterialCalendarView materialCalendarView;

    static CalendarDay selectedDay = null;
    static boolean Selected;

    ArrayAdapter<String> adapter;
    ArrayList<String> arrayList;
    ArrayList<DayData> Day_data;

    EditText edit_schedule;
    ListView schedule_List;
    TimePicker timePicker;
    String DATE;

    int year;
    int month;
    int day;
    int hour;
    int min;
    String AMPM = "";
    String text_schedule = "";

    @Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Day_data = new ArrayList<DayData>();

//        edit_schedule = (EditText)getView().findViewById(R.id.edit_schedule) ;
//        edit_hour = (EditText)getView().findViewById(R.id.edit_hour);
//        edit_min = (EditText)getView().findViewById(R.id.edit_min);
        schedule_List = (ListView)getView().findViewById(R.id.list_schedule);

        materialCalendarView = (MaterialCalendarView)getView().findViewById(R.id.calendarView);
        materialCalendarView.addDecorators(
                new SundayDecorator(),          //일요일 색 설정
                new SaturdayDecorator(),        //토요일 색 설정
                new todayDecorator());

        materialCalendarView.setOnDateChangedListener(new OnDateSelectedListener() {
            @Override
            public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {

                if(selectedDay == date){
                    selected = false;
                    Selected = selected;
                }
                else{
                    selected = true;
                    Selected = selected;
                }
                selectedDay = date;
                DATE = selectedDay.toString();
                String[] parsedDATA = DATE.split("[{]");
                parsedDATA = parsedDATA[1].split("[}]");
                parsedDATA = parsedDATA[0].split("-");
                year = Integer.parseInt(parsedDATA[0]);
                month = Integer.parseInt(parsedDATA[1])+1;
                day = Integer.parseInt(parsedDATA[2]);

                arrayList = new ArrayList<String>();
                for(int i=0; i<Day_data.size(); i++){
                    if(Day_data.get(i).getDay() == day){
                        arrayList.add(Day_data.get(i).getText_schedule());
                    }
                }
                updateScheduleList();
//                final Dialog dialog = new Dialog(getActivity());
//                dialog.setCancelable(true);
//
//                View v = getActivity().getLayoutInflater().inflate(R.layout.add_schedule,null);
//                dialog.setContentView(v);
//                dialog.setTitle(""+date);
//
//                edit_schedule = (EditText)dialog.findViewById(R.id.edit_schedule);
//                timePicker = (TimePicker)dialog.findViewById(R.id.timepicker);
//
//                Button ok = (Button)dialog.findViewById(R.id.add_schedule);
//                ok.setOnClickListener(new OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        text_schedule = edit_schedule.getText().toString();
//                        hour = timePicker.getCurrentHour();
//                        min = timePicker.getCurrentMinute();
//
//                        if(hour >12){
//                            AMPM = "PM";
//                            hour = hour - 12;
//                        }else if(hour == 0){
//                            AMPM = "AM";
//                            hour = 12;
//                        }else if(hour == 12){
//                            AMPM = "PM";
//                        }else{
//                            AMPM = "AM";
//                        }
//                        dialog.dismiss();
//                    }
//                });
//                Button cancel = (Button)dialog.findViewById(R.id.cancel_schedule);
//                cancel.setOnClickListener(new OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        dialog.dismiss();
//                    }
//                });
//                dialog.show();
//            }
//        });
        Button add = (Button)getView().findViewById(R.id.make_schedule);
        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Selected){
                    final DayData dd = new DayData();
                    final Dialog dialog = new Dialog(getActivity());
                    dialog.setCancelable(true);

                    View viewDialog = getActivity().getLayoutInflater().inflate(R.layout.add_schedule,null);
                    dialog.setContentView(viewDialog);
                    if(month < 10){
                        dialog.setTitle(year+"/0"+month+"/"+day);
                    }else{
                        dialog.setTitle(year+"/"+month+"/"+day);
                    }
                    edit_schedule = (EditText)dialog.findViewById(R.id.edit_schedule);
                    timePicker = (TimePicker)dialog.findViewById(R.id.timepicker);

                    Button ok = (Button)dialog.findViewById(R.id.add_schedule);
                    ok.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            text_schedule = edit_schedule.getText().toString();
                            hour = timePicker.getCurrentHour();
                            min = timePicker.getCurrentMinute();

                            if(hour >12){
                                AMPM = "PM";
                                hour = hour - 12;
                            }else if(hour == 0){
                                AMPM = "AM";
                                hour = 12;
                            }else if(hour == 12){
                                AMPM = "PM";
                            }else{
                                AMPM = "AM";
                            }
                            dd.setTime(hour,min);
                            dd.setString(text_schedule);

                            dialog.dismiss();
                        }
                    });
                    Button cancel = (Button)dialog.findViewById(R.id.cancel_schedule);
                    cancel.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                    dialog.show();
                }else{
                    Toast.makeText(getActivity(),"Select date to add schedule",Toast.LENGTH_LONG).show();
                }

            }
        });
            }
        });
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View rootview = inflater.inflate(R.layout.second,container,false);
        return rootview;
    }

    private class SundayDecorator implements DayViewDecorator{
        private final Calendar calendar = Calendar.getInstance();

        private SundayDecorator(){
        }

        @Override
        public boolean shouldDecorate(CalendarDay day) {
            day.copyTo(calendar);
            int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
            return weekDay == Calendar.SUNDAY;
        }

        @Override
        public void decorate(DayViewFacade view) {
            view.addSpan(new ForegroundColorSpan(Color.RED));
        }
    }

    private class SaturdayDecorator implements DayViewDecorator{
        private final Calendar calendar = Calendar.getInstance();

        private SaturdayDecorator(){
        }

        @Override
        public boolean shouldDecorate(CalendarDay day) {
            day.copyTo(calendar);
            int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
            return weekDay == Calendar.SATURDAY;
        }

        @Override
        public void decorate(DayViewFacade view) {
            view.addSpan(new ForegroundColorSpan(Color.BLUE));
        }
    }

    private class todayDecorator implements DayViewDecorator{
        private CalendarDay date;
        private todayDecorator(){
            date = CalendarDay.today();
        }

        @Override
        public boolean shouldDecorate(CalendarDay day) {
            return date != null && day.equals(date);
        }

        @Override
        public void decorate(DayViewFacade view) {
            view.addSpan(new StyleSpan(Typeface.BOLD));
            view.addSpan(new RelativeSizeSpan(1.4f));
            view.addSpan(new ForegroundColorSpan(Color.rgb(150,0,200)));
        }

        public void setDate(Date date){
            this.date = CalendarDay.from(date);
        }
    }

    private static class ClickDecorator implements DayViewDecorator{

        @Override
        public boolean shouldDecorate(CalendarDay day) {
            return false;
        }

        @Override
        public void decorate(DayViewFacade view) {

        }
    }


    private static class EventDecorator implements DayViewDecorator{
        private int color;
        private HashSet<CalendarDay> dates;

        private EventDecorator(int color, Collection<CalendarDay> dates){
            this.color = color;
            this.dates = new HashSet<>(dates);
        }

        @Override
        public boolean shouldDecorate(CalendarDay day) {
            return dates.contains(day);
        }

        @Override
        public void decorate(DayViewFacade view) {
            view.addSpan(new DotSpan(5,color));
        }
    }

    public void updateScheduleList(){
        adapter= new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,arrayList);
        schedule_List.setAdapter(adapter);
    }

    public static class ApiSimulator extends AsyncTask<Void, Void, List<CalendarDay>>{

        @Override
        protected List<CalendarDay> doInBackground(Void... params) {
            try{
                Thread.sleep(2000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            Calendar calendar = Calendar.getInstance();                     //Event 가진 곳에 대한 작동
            calendar.add(Calendar.MONTH, -2);
            ArrayList<CalendarDay> dates = new ArrayList<>();
            for(int i = 0; i < 30; i++){
                CalendarDay day = CalendarDay.from(calendar);
                dates.add(day);
                calendar.add(Calendar.DATE, 5);
            }
            return dates;
        }

        @Override
        protected void onPostExecute(List<CalendarDay> calendarDays) {
            super.onPostExecute(calendarDays);
            materialCalendarView.addDecorator(new EventDecorator(Color.RED,calendarDays));
        }
    }
}

  • 2017년 12월 29일에 작성됨
    IT업계에 대해서 배우고 있는 학생입니다. 현재 졸업준비 중에 있습니다.

조회수 71


로그인이 필요한 기능입니다.

Hashcode는 개발자들을 위한 무료 QnA사이트 입니다. 작성한 답변에 다른 개발자들이 댓글을 작성하거나 좋아요/싫어요를 할 수 있기 때문에 계정을 필요로 합니다.
► 로그인
► 계정만들기
Close