使用Items填充列表以在MaterialCalendarView中显示MonthWise?

时间:2017-07-02 05:22:52

标签: android json listview android-calendar

如何通过网络按月填充列表事件。随着日历月的更改,列表应该刷新。我现在正在收到所有事件。可以解决这个问题吗?

CalenderFragment

  public class CalenderFragment extends Fragment implements OnDateSelectedListener, OnMonthChangedListener {
        String Navigation_URL = "http://192.168.100.5:84/api/academics/getEvents";
        String access_token;
        ListView listView;
        com.prolificinteractive.materialcalendarview.MaterialCalendarView calendarView;
        ArrayList<CalenderPojoStudent> student_list_calender = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();

        List<CalendarDay> calendarDays;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        String y;


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


            View view = inflater.inflate(R.layout.calender, container, false);
            getActivity().setTitle("");
            setHasOptionsMenu(true);
            //   calendar = (CalendarView) view.findViewById(R.id.calendar);
            /*     calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

                @Override
                public void onSelectedDayChange(CalendarView view,
                                                int year, int month, int dayOfMonth) {
                    Toast.makeText(getContext(),
                            dayOfMonth + "/" + month + "/" + year, Toast.LENGTH_LONG).show();


                }
            });   */

            calendarView = (com.prolificinteractive.materialcalendarview.MaterialCalendarView) view.findViewById(R.id.calendar);
            //  calendarView.addDecorator(new HighlightWeekendsDecorator());
            calendarView.addDecorator(new OneDayDecorator());
            // calendarView.addDecorator(new EventDecorator(Color.RED, calendarDays));
            // calendarView.addView(onMonthChanged(calendarView,date));
            // calendarView.addDecorator(new EventDecorator(Color.RED, calendarDays));
            //calendarView.addDecorator(new MySelectorDecorator(getActivity()));


            // calendarView.addDecorator(new EventDecorator("#FF0000",student_list_calender));
            //  calendarView.addDecorator(new EventDecorator("",));
            calendarView.setDateTextAppearance(View.ACCESSIBILITY_LIVE_REGION_ASSERTIVE);
            final Calendar calendar = Calendar.getInstance();
            calendarView.setSelectedDate(calendar.getTime());


            calendarView.setOnMonthChangedListener(this);
            //  calendarView.setOnDateChangedListener(this);


            calendarView.setOnDateChangedListener(new OnDateSelectedListener() {

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


                    CalenderAdapter calenderAdapter = new CalenderAdapter(getActivity(), student_list_calender);
                    listView.setAdapter(calenderAdapter);


                    calendarView.setHeaderTextAppearance(R.style.AppTheme);
                }
            });


            listView = (ListView) view.findViewById(R.id.calenderlist);
            SessionManagement session = new SessionManagement(getContext());
            session.checkLogin();
            access_token = session.getAccesstToken();
            makeJsonObjectRequest();


            return view;
        }

        List<Event> events = new ArrayList<>();


        private void makeJsonObjectRequest() {

            RequestQueue requestQueue = Volley.newRequestQueue(getContext());
            String URL = Navigation_URL;

            StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            try {


                                JSONArray jArray = new JSONArray(response);
                                for (int i = 0; i < jArray.length(); i++) {
                                    JSONObject jsonObject = jArray.getJSONObject(i);
                                    String StartDate = jsonObject.getString("StartDate").substring(0, 10);
                                    String Title = jsonObject.getString("Title");
                                    int EventID = jsonObject.getInt("EventID");


                                    //String y = "#" + EventID;
                                    //int z = Integer.parseInt(y);
                                    //int y = Integer.parseInt(("#" + "2493A7"));
                                    // int colorcrop = Integer.parseInt("#2493A7");
                                    // int y = Integer.parseInt("#" + ("2493A7"));


                                    CalendarDay date = new CalendarDay();
                                    y = (simpleDateFormat.format(date.getDate()));
                                    System.out.println(y);
                                    // OnMonthChangedListener onMonthChangedListener=getActivity().;


                                    Toast.makeText(getContext(), y, Toast.LENGTH_SHORT).show();

                                    //  if (y == StartDate) {
                                    student_list_calender.add(new CalenderPojoStudent(StartDate, Title));
                                    // }





                                  /*  try {
                                        Date date = simpleDateFormat.parse(StartDate);
                                        //   String listofinformation = (EventID);
                                        int color = Integer.parseInt(String.valueOf(EventID));

                                        try {
                                            // int y = Integer.parseInt("#" + Integer.parseInt(String.valueOf(color)));
                                            int y = Integer.parseInt(String.valueOf(Integer.parseInt(String.valueOf(color))));
                                            Event event = new Event(date, y);
                                            events.add(event);


                                        } catch (Exception e) {
                                            Log.e("Exception", e.toString());
                                        }

                                    } catch (ParseException e) {
                                        e.printStackTrace();
                                    }   */


                                }

                                CalenderAdapter calenderAdapter = new CalenderAdapter(getActivity(), student_list_calender);
                                listView.setAdapter(calenderAdapter);


                            } catch (JSONException e) {
                                makeText(getContext(), "Fetch failed!", LENGTH_SHORT).show();
                                e.printStackTrace();
                            }
    /*
                            for (Event event : events) {
                                EventDecorator eventDecorator = new EventDecorator(calendarView, event.getDate(), event.getColor());
                                calendarView.addDecorator(eventDecorator);

                                EventDecorator eventDecorator = new EventDecorator(event);


                            }   */


                        }

                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    makeText(getContext(), error.toString(), LENGTH_LONG).show();
                }
            }) {

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String> headers = new HashMap<String, String>();
                    headers.put("Authorization", "Bearer " + access_token);
                    headers.put("Content-Type", "application/x-www-form-urlencoded");
                    return headers;
                }
    /*
                @Override
                protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
                    Map<String, String> map = new HashMap<String, String>();

                    map.put("id", master_id);
                    map.put("accessID", accessID);
                    map.put("currentUser", master_id);
                    return map;

                } */
            };


            requestQueue.add(stringRequest);

        }


        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            // TODO Auto-generated method stub
            super.onCreateOptionsMenu(menu, inflater);
            inflater.inflate(R.menu.dashboard, menu);
        }


        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // handle item selection
            switch (item.getItemId()) {
                case R.id.action_settings:
                    // do s.th.
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
            }
        }


        @Override
        public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
            //noinspection ConstantConditions
            //getSupportActionBar().setTitle(FORMATTER.format(date.getDate()));

            // Toast.makeText(getContext(), "Second Changed", Toast.LENGTH_LONG).show();


            // CalenderAdapter calenderAdapter = new CalenderAdapter(getActivity(), student_list_calender);
            // listView.setAdapter(calenderAdapter);


            // int year = calendar.get(Calendar.YEAR);
            // int month = calendar.get(Calendar.MONTH);
            //int day = calendar.get(Calendar.DAY_OF_MONTH);

            //System.out.println(year);
            //System.out.println(month);
            //System.out.println(day);


            y = (simpleDateFormat.format(date.getDate()));
            System.out.println(y);


            // date = widget.getSelectedDate();


            //widget.setDateSelected(date,true);


            // widget.setShowOtherDates(MaterialCalendarView.SHOW_ALL);

            // System.out.println("date is" + date);


            widget.invalidateDecorators();
            //  makeJsonObjectRequest();
        }


        @Override
        public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
            OneDayDecorator oneDayDecorator = new OneDayDecorator();
            oneDayDecorator.setDate(date.getDate());

            //date = widget.getSelectedDate();
            //Log.d("date","displyed");
            //  widget.addDecorator(new EventDecorator(Color.DKGRAY, calendarDays));


            //  date = widget.getSelectedDate();
            //  System.out.println(date);


            if (selected == true) {
                //It can't be show
                Toast.makeText(getContext(), "onClick" + date, Toast.LENGTH_SHORT).show();
            }


            widget.invalidateDecorators();
        }


    }

JSON

[
 {
    "EventID": 2095,
    "Title": "Holiday",
    "Description": "",
    "StartDate": "2017-06-27T00:00:00",
    "StartTime": "00:00:00",
    "EndDate": "2017-06-28T00:00:00",
    "EndTime": "00:00:00",
    "IsFullDay": true,
    "IsHoliday": true,
    "DateTime": null
  },
  {
    "EventID": 2097,
    "Title": "Art Exhibition",
    "Description": "asd",
    "StartDate": "2017-06-28T00:00:00",
    "StartTime": "12:07:00",
    "EndDate": "2017-06-28T00:00:00",
    "EndTime": "17:07:00",
    "IsFullDay": false,
    "IsHoliday": false,
    "DateTime": null
  }
]
  

如何将材料日历日期与Web日期和显示比较   monthWise?

0 个答案:

没有答案