在特定月份显示特定事件?

时间:2017-04-05 04:26:21

标签: android listview calendar

我正在使用CalenderView显示日期和下面的CalenderView我使用ListView来显示带有Events的日期(日)。我能够解析所有日期事件但我想要的是显示特定日特定月份下的活动。如何实现?

CalenderFragment

public class CalenderFragment extends Fragment {
    CalendarView calendar;
    String Navigation_URL = "http://192.168.100.5:84/api/academics/getEvents";
    String access_token;
    ListView listView;


    @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();
            }
        });


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

        makeJsonObjectRequest();

        return view;
    }

    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 {

                            ArrayList<CalenderPojoStudent> student_list_calender = new ArrayList<>();

                            JSONArray jArray = new JSONArray(response);
                            for (int i = 0; i < jArray.length(); i++) {
                                JSONObject jsonObject = jArray.getJSONObject(i);
                                String StartAt =  jsonObject.getString("StartAt").substring(6,10);
                                String Title =  jsonObject.getString("Title");
                                student_list_calender.add(new CalenderPojoStudent(StartAt, Title));
                            }
                            CalenderAdapter calenderAdapter = new CalenderAdapter(getActivity(), student_list_calender);
                            listView.setAdapter(calenderAdapter);


                        } catch (JSONException e) {
                            Toast.makeText(getContext(), "Fetch failed!", Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }
                    }

                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getContext(), error.toString(), Toast.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);
        }
    }

}

CalenderAdapter

public class CalenderAdapter extends BaseAdapter {


    Context mContext;

    ArrayList<CalenderPojoStudent> student_class_calender = null;
    String TAG = "HomeTab_adapter";

    public CalenderAdapter(Context mContext, ArrayList<CalenderPojoStudent> student_class_calender) {

        this.mContext = mContext;
        this.student_class_calender = student_class_calender;
    }

    @Override
    public int getCount() {
        //   System.out.println(student_class_calender.size());
        return student_class_calender.size();

    }

    @Override
    public Object getItem(int position) {
        return student_class_calender.get(position);
        // return arg0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int postion, View convertView, ViewGroup parent) {
        // final ClassScheduleStudentAdapter.Holder viewHolder;
        final Holder viewHolder;

        if (convertView == null) {
            // inflate the layout
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.calender_student_listitems, parent, false);

            // well set up the ViewHolder
            //  viewHolder = new ClassScheduleStudentAdapter.Holder();
            viewHolder = new Holder();
            viewHolder.student_calender_date = (TextView) convertView.findViewById(R.id.student_calender_date);
            viewHolder.student_calender_events = (TextView) convertView.findViewById(R.id.student_calender_events);

            convertView.setTag(viewHolder);


        } else {
            // we've just avoided calling findViewById() on resource everytime
            // just use the viewHolder
            //  viewHolder = (ClassScheduleStudentAdapter.Holder) convertView.getTag();
            viewHolder = (CalenderAdapter.Holder) convertView.getTag();
        }
        // Log.d(TAG, "@@ postion:" + postion + " getTeacherName" + student_class_schedule.get(postion).getTeacherName());
        viewHolder.student_calender_date.setText(student_class_calender.get(postion).getDate());
        viewHolder.student_calender_events.setText(student_class_calender.get(postion).getEvents());


        // convertView.setTag(viewHolder);
        return convertView;
    }

    class Holder {


        TextView student_calender_date;
        TextView student_calender_events;


    }

}

CalenderPojoStudent

public class CalenderPojoStudent {
    String Date;
    String Events;

    public CalenderPojoStudent(String date, String events) {
        Date = date;
        Events = events;
    }

    public String getDate() {
        return Date;
    }

    public String getEvents() {
        return Events;
    }

    public void setEvents(String events) {
        Events = events;
    }

    public void setDate(String date) {
        Date = date;

    }
}

calender_student_listitems

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="center_horizontal"
    android:background="#DAE2E6"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/student_calender_date"
        android:layout_width="50dp"
        android:layout_height="40dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:background="#fff"
        android:gravity="center_horizontal"
        android:paddingTop="7dp"
        android:text="04"
        android:textColor="#008b8b"
        android:textSize="20dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/student_calender_events"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="8dp"
        android:text="Sports Day"
        android:textSize="17dp"
        android:textStyle="bold" />


</LinearLayout>

压延

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#2E353D"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="30dp"
                android:layout_height="match_parent"
                android:padding="3dp"
                android:src="@mipmap/calander" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="8dp"
                android:text="Calendar"
                android:textColor="#fff"
                android:textSize="17dp"
                android:textStyle="bold" />

        </LinearLayout>
    </LinearLayout>

    <CalendarView
        android:id="@+id/calendar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />


    <ListView
        android:id="@+id/calenderlist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

JSON

[
  {
    "EventID": 2,
    "Title": "Event1",
    "Description": "gdfg",
    "StartAt": "2017-03-01T00:00:00",
    "EndAt": "2017-03-02T05:45:00",
    "IsFullDay": true,
    "IsHoliday": false
  },
  {
    "EventID": 4,
    "Title": "f",
    "Description": "sdf",
    "StartAt": "2017-03-02T09:52:00",
    "EndAt": "2017-03-02T10:52:00",
    "IsFullDay": false,
    "IsHoliday": false
  },
  {
    "EventID": 3,
    "Title": "Event3",
    "Description": "dsf",
    "StartAt": "2017-03-03T00:00:00",
    "EndAt": "2017-03-04T05:45:00",
    "IsFullDay": true,
    "IsHoliday": true
  },
  {
    "EventID": 10,
    "Title": "xcgz",
    "Description": "zxcv",
    "StartAt": "2017-04-02T00:00:00",
    "EndAt": "2017-04-03T00:00:00",
    "IsFullDay": true,
    "IsHoliday": false
  },
  {
    "EventID": 9,
    "Title": "xzcvzx",
    "Description": "vxzvc",
    "StartAt": "2017-04-04T00:00:00",
    "EndAt": "2017-04-05T00:00:00",
    "IsFullDay": true,
    "IsHoliday": false
  },
  {
    "EventID": 14,
    "Title": "afagag",
    "Description": "",
    "StartAt": "2017-04-04T00:00:00",
    "EndAt": "2017-04-05T00:00:00",
    "IsFullDay": true,
    "IsHoliday": true
  }
]

如何在Calender Month的basais上的listView上显示特定月份的事件?

1 个答案:

答案 0 :(得分:0)

您可以使用此库来显示日历中的事件.DOts将显示在日期下方。 MaterialCalendarView
搜索装饰器,你可以在该库中使用wth日期。

materialCalendarView.setDateTextAppearance()
materialCalendarView.addDecorator(new OpenSansDecorator(getActivity()));  
      materialCalendarView.setHeaderTextAppearance(R.style.TextAppearance_ProfileCalenderHeader);

这可以用来在日历中显示点,你可以实现一个逻辑,根据日历中的日期点击显示recylerview。

请参阅this以了解lib中的事件装饰: