如何在MaterialCalendarView中显示特定日期和时间的事件? [英] How do I display events to a specific date and time in a MaterialCalendarView?

查看:153
本文介绍了如何在MaterialCalendarView中显示特定日期和时间的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试一个示例应用程序,试图在该应用程序中显示特定日期的时间和日期.目前,即使事件是一天,它也只能绘制到整个月份.假设事件发生在1月5日上午9:30到10:30,那么它将显示整个月,而不仅仅是1月5日.想要实现这样的目标:

I am testing out a sample app where I am trying to achieve displaying time and date to a specific day.Currently, it just plots to the entire month even if the event if for a day. Say if the event if from 9:30am to 10:30am on Jan 5, it will show up for the entire month, rather than just for Jan 5th. Want to achieve something like this:

目前,即使我的应用程序显示了1月1日或1月8日事件,我的应用程序也会显示整个月的时间(例如,以下屏幕截图显示了该事件在1月15日这一周的错误显示,即使这2个事件属于2个不同的日期):

Currently my app shows it for the whole month, even if its jan 1 or jan 8 event ( here's a screenshot where it wrongly shows up on the week of jan 15 for instance, even if these 2 events belong to 2 different dates):

我正在测试的代码在这里: https://github.com /raghunandankavi2010/SamplesAndroid/tree/master/CalendarTest

The code I am testing out, is here : https://github.com/raghunandankavi2010/SamplesAndroid/tree/master/CalendarTest

唯一的区别是,我在calendar.xml类中的位置添加了<com.prolificinteractive.materialcalendarview.MaterialCalendarView下的app:mcv_showOtherDates="all"以显示每周视图. 另外,在calendarFragment.java类中,有一个名为makeJsonObjectRequest的函数,在其中解析了StartDate,我尝试解析startTime,StartTime,EndTime和EndDate,以将事件分别映射到日期,但是并没有做很多事情.

With the only difference where in the calendar.xml class I added : app:mcv_showOtherDates="all" under <com.prolificinteractive.materialcalendarview.MaterialCalendarView to show a weekly view. Also, in calendarFragment.java class there is a function called : makeJsonObjectRequest , where the StartDate is parsed, I tried parsing the startTime StartTime and EndTime and EndDate to map the event respective to the date, but that didn't do much.

 String EndDate = jsonObject.getString("EndTime");
                Date date = simpleDateFormat.parse(EndDate);

                String title =  jsonObject.getString("Title");

                Log.d("EndDate ",""+date);
                CalendarDay day = CalendarDay.from(date);
                Event event = new Event(date,title);
                cal = Calendar.getInstance();
                cal.setTime(date);
                int month = cal.get(Calendar.DAY_OF_THE_MONTH); 

任何想法都可以解决并更新代码,这样我就可以显示对应于当月月份和特定时间范围内的事件(如上面的Outlook屏幕快照(第一个屏幕快照)所示),而不是整个事件月? (此外,json类为testjson.json并包含在项目资产文件夹中)

Any idea how to go about this and update the code so I can display the event respective to the day of the month and within specific time frame (as shown in the outlook screenshot(first screenshot) above), instead of the whole month? (Also, the json class i testjson.json and included in the project assets folder)

谢谢!

推荐答案

回购中的示例使用月份过滤事件.您需要按日历日过滤事件并在onDateSelected中更新列表视图,以便在用户选择其他日期时更新列表视图.你应该得到这样的东西:

The example in the repo filters the events using the months. You need to filter events by calendar day and update listview in onDateSelected to update listview whenever user selects a different date. You should get something like this:

public class CalendarFragment extends Fragment implements OnDateSelectedListener {


    private MaterialCalendarView calendarView;
    private HashMap<CalendarDay,List<Event>> map = new HashMap<>();
    private ListView listView;
    private MyAdapter adapter;


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

        View view = inflater.inflate(R.layout.calendar, container, false);

        listView = view.findViewById(R.id.listview);

        adapter = new MyAdapter(getActivity(),new ArrayList<Event>());
        listView.setAdapter(adapter);

        calendarView =  view.findViewById(R.id.calendarView);
        calendarView.setDateTextAppearance(View.ACCESSIBILITY_LIVE_REGION_ASSERTIVE);

        calendarView.setSelectedDate(LocalDate.now());

        calendarView.setOnDateChangedListener(this);

        makeJsonObjectRequest();

        return view;
    }




    private void makeJsonObjectRequest() {

        String response = loadJSONFromAsset();
        try {
            JSONArray jArray = new JSONArray(response);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject jsonObject = jArray.getJSONObject(i);
                String StartDate = jsonObject.getString("StartDate");
                LocalDate date = LocalDate.parse(StartDate, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss",Locale.US));

                String title =  jsonObject.getString("Title");

                Log.d("Date ",""+date);
                CalendarDay day = CalendarDay.from(date);
                Event event = new Event(date,title);


                if(!map.containsKey(day))
                {
                    List<Event> events = new ArrayList<>();
                    events.add(event);
                    map.put(day,events);
                }else
                {
                    List<Event> events = map.get(day);
                    events.add(event);
                    map.put(day,events);

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // after parsing
        List<Event> event =  map.get(CalendarDay.from(LocalDate.now()));
        if(event!=null && event.size()>0) {
            adapter.addItems(event);
        }else {
            adapter.clear();
        }

        //add small dots on event days
        EventDecorator eventDecorator = new EventDecorator(Color.RED, map.keySet());
        calendarView.addDecorator(eventDecorator);


    }

    public String loadJSONFromAsset() {
        String json = null;
        try {
            InputStream is = getActivity().getAssets().open("testjson.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;
    }

    @Override
    public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {

        calendarView.setHeaderTextAppearance(R.style.AppTheme);

        List<Event> event =  map.get(date);
        if(event!=null && event.size()>0) {
            adapter.addItems(event);
        }else {
            adapter.clear();
        }
    }

}

这篇关于如何在MaterialCalendarView中显示特定日期和时间的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆