Android: Calendar with Events

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-15 05:32:47

问题


I have some issue regarding calendar in android. I want to add events which is on database. What I basically want is when i open my calendar fragment it will call web service and fetch the data from the server which includes dates and their respective events when i click on that date in calendar it shows me the events on specified date. Issues which i faced are:

  1. It shows some error on this line date = sdf.parse(dates); and it says

    java.lang.NullPointerException
       at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1009)
       at java.text.DateFormat.parse(DateFormat.java:553)
       at com.example.aksystems.practiceportal.Calendar.showJSON(Calendar.java:118)
    
  2. Two days before it runs successfully and the issue is it will generate point on calendar day twice. Example: my server response is

    [{"d":"a","events":"2017\/04\/13 18:20:43"},{"d":"a","events":"2017\/04\/10 18:20:40"}]`
    

    so it will point date 13 once and date 10 so many times why?

Calendar.java

public class Calendar extends Fragment {

    CalendarView calendarView;

    private EditText editTextId;
    private Button buttonGet;
    private TextView textViewResult;
    String dates,events;
    private ProgressDialog loading;
    CompactCalendarView compactCalendarView;
    String id = "a";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = null;
    public Calendar() {}

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

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

        compactCalendarView = (CompactCalendarView) view.findViewById(R.id.compactcalendar_view);

        editTextId = (EditText) view.findViewById(R.id.editTextId);
        buttonGet = (Button)view. findViewById(R.id.buttonGet);
        textViewResult = (TextView) view.findViewById(R.id.textViewResult);
        getData();

        return view;

    }
    private void getData() {


        loading = ProgressDialog.show(getActivity(),"Please wait...","Fetching...",false,false);

        String url = Config.DATA_URL+"?e="+id;

        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>()
        {
            @Override
            public void onResponse(String response) {
                loading.dismiss();
                showJSON(response);
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getActivity(),error.getMessage().toString(),Toast.LENGTH_LONG).show();
                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
        requestQueue.add(stringRequest);
    }

    private void showJSON(String response)
    {

        for (int i=0;i < response.length();i++)

        {
            try {

                JSONObject jsonObject = new JSONObject(response);
                JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
                JSONObject collegeData = result.getJSONObject(i);
                dates = collegeData.getString(Config.KEY_DATES);
                events = collegeData.getString(Config.KEY_EVENTS);
                //vc = collegeData.getString(Config.KEY_VC);*/

            } catch (JSONException e) {
                e.printStackTrace();
            }
            // textViewResult.setText(""+dates);

            // String myDate = "2017/03/30 18:10:45";

            try
            {
                date = sdf.parse(dates);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            // Convert Date into milliseconds
            assert date != null;
            long millis = date.getTime();

            Event ev1 = new Event(Color.RED, millis, events);
            compactCalendarView.addEvent(ev1);
           /* List<Event> events = compactCalendarView.getEvents(millis);
            Toast.makeText(getActivity(), "" + events, Toast.LENGTH_SHORT).show();*/
            compactCalendarView.setListener(new CompactCalendarView.CompactCalendarViewListener()
            {
                @Override
                public void onDayClick(Date dateClicked) {
                    List<Event> events = compactCalendarView.getEvents(dateClicked);

                    // Log.d(TAG, "Day was clicked: " + dateClicked + " with events " + events);
                    Toast.makeText(getActivity(), "" + events, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onMonthScroll(Date firstDayOfNewMonth)
                {
                    //  Log.d(TAG, "Month was scrolled to: " + firstDayOfNewMonth);
                   // Toast.makeText(getActivity(), "Month was scrolled to: " + firstDayOfNewMonth, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}

回答1:


  1. This is because your passed date is not in current format. See your PHP script.
  2. Your varibleresponse.length() returns value more than two so it will point to more than one events in last date. Try using JSONArray.size() to find the length of response.


来源:https://stackoverflow.com/questions/43262036/android-calendar-with-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!