1

I am able to show the Calendar with CalendarView but got stuck to add events within it.Can we add events to the date in the calendar through the JSON Data.If yes how can this be achieved? if not then what would be the alternative solution?I got stuck in it for more than 2 day.please help

calendar xml

<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>

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

}

How can events be added within calendar through JSON Data?

Ghimire
  • 948
  • 1
  • 9
  • 28

1 Answers1

0

Let me understand your question , you want Add event form json data? if yes then your need to do

  1. Parse JSON data

  2. Add event in calendar using Content Provider (here is some links can help or you can search in google Calendar Content provider)

https://developer.android.com/guide/topics/providers/calendar-provider.html

How to read and edit Android calendar events using the new Android 4.0 Ice Cream Sandwich API?

suggesting first study about android content provider if you don't have knowledge about. https://developer.android.com/guide/topics/providers/content-providers.html

Sorting of data :

  1. if there is only single value data then can do sorting very simple way http://beginnersbook.com/2013/12/how-to-sort-arraylist-in-java/

  2. if complex data then you needed model class to store data and use any object for sorting (one example i posting rest you can search in google) Sort ArrayList<class> in Java

Community
  • 1
  • 1
R kanojia
  • 105
  • 4
  • 17
  • i have done almost all the work.I have created listView below CalendarView and able to display events.But i want to display specific events according to month.Can this be done.I am getting all the events right now.#edited please check. i am getting this right now – Ghimire Apr 07 '17 at 06:09
  • If you put your data in calendar then show event month wise easy but i can see you are showing data directly in list. So you need use data structure and sorting. I have one app as same concept but i used calendar api (https://play.google.com/store/apps/details?id=oca.eyehunt.in.oca) – R kanojia Apr 07 '17 at 06:49
  • can you guide me how to use datastructue and sorting – Ghimire Apr 07 '17 at 08:23
  • @Ghimire sure , i updated answer and let me know if any doubt, All the best ! – R kanojia Apr 07 '17 at 12:58