Android将日历事件添加到特定日期

时间:2013-08-19 07:53:29

标签: android android-calendar

我正在尝试向日历中添加事件,我有一个自定义日历,如果我想添加事件我将点击日期网格,它打开手机内置日历添加事件,我面临的问题是文日历打开以添加事件,显示当前日期,自动填写,即使我点击其他日期。如何获取我在添加活动日历中点击的日期。

gridview.setOnItemClickListener(new OnItemClickListener() {
                @SuppressLint("NewApi") public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {

                    desc = new ArrayList<String>();

                    date = new ArrayList<String>();
                    ((CalendarAdapter) parent.getAdapter()).setSelected(v);
                    String selectedGridDate = CalendarAdapter.dayString
                            .get(position);
                    String[] separatedTime = selectedGridDate.split("-");
                    String gridvalueString = separatedTime[2].replaceFirst("^0*",
                            "");// taking last part of date. ie; 2 from 2012-12-02.
                    int gridvalue = Integer.parseInt(gridvalueString);
                    // navigate to next or previous month on clicking offdays.
                    if ((gridvalue > 10) && (position < 8)) {
                        setPreviousMonth();
                        refreshCalendar();
                    } else if ((gridvalue < 7) && (position > 28)) {
                        setNextMonth();
                        refreshCalendar();
                    }
                    ((CalendarAdapter) parent.getAdapter()).setSelected(v);

                    for (int i = 0; i < Utility.startDates.size(); i++) {
                        if (Utility.startDates.get(i).equals(selectedGridDate)) {
                            desc.add(Utility.nameOfEvent.get(i));
                        }
                    }

                    if (desc.size() > 0) {
                        for (int i = 0; i < desc.size(); i++) {

                            // set some properties of rowTextView or something
                            rowTextView.setText("Event on: "+selectedGridDate +": " + desc.get(i));
                            //rowTextView.setTextColor(Color.BLUE);

                            // final ListView listView = (ListView) view.findViewById(R.id.list);

                            quickAction.show(v);

                        }

                    }else {

                        /**
                         *  Dialog dialog = new Dialog(getActivity());

                           dialog.setContentView(R.layout.addeventdialog);
                           dialog.setTitle("Events");
                           TextView showdate = (TextView) dialog.findViewById(R.id.showdate);                   
                           showdate.setText(selectedGridDate);
                          dialog.show();     */

                        Calendar cal = Calendar.getInstance(); 
                        GregorianCalendar calDate = new GregorianCalendar();
                        if (Build.VERSION.SDK_INT >= 14) {

                            Intent intent = new Intent(Intent.ACTION_INSERT)
                            .setData(Events.CONTENT_URI)
                            .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,calDate.getTimeInMillis())
                            .putExtra(CalendarContract.EXTRA_EVENT_END_TIME,calDate.getTimeInMillis())
                            .putExtra(Events.TITLE, "Yoga")
                            .putExtra(Events.DESCRIPTION, "Group class")
                            .putExtra(Events.EVENT_LOCATION, "The gym")
                            .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY)
                            .putExtra(Intent.EXTRA_EMAIL, "rowan@example.com,trevor@example.com");
                             startActivity(intent);
                    } else {

                        Intent intent = new Intent(Intent.ACTION_INSERT);
                        intent.setType("vnd.android.cursor.item/event");
                        intent.putExtra("beginTime", cal.getTimeInMillis());
                        intent.putExtra("allDay", false);
                        intent.putExtra("rrule", "FREQ=DAILY");
                        intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
                        intent.putExtra("title", "A Test Event from android app");


                        startActivity(intent);

                    }
                    }
                    desc = null;

                }

            });
            return view;



        }

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

selectedGridDate 值传递给下面的方法后 示例:String date = "11-August-2013"

public long getMilliSeconds(String date) throws ParseException{
    Date dateSample = null ; 
    try {  
        SimpleDateFormat formatter ;       
        formatter = new SimpleDateFormat("dd-MMM-yyyy");
        dateSample = (Date) formatter.parse(date);  
    } catch (Exception e) {
    }  

    return dateSample.getTime();
}

一旦你有毫秒

//假设你有这种格式!!

long startTime = getMilliSeconds("11-August-2013") + 1000 * 60 * 60;  
long endTime = getMilliSeconds("11-August-2013") + 1000 * 60 * 60 * 2;//add required delay

将其添加到意图:

intent.putExtra("beginTime", startTime);
intent.putExtra("endTime",endTime);

检查一下:

gridview.setOnItemClickListener(new OnItemClickListener() {

    @SuppressLint("NewApi")
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

        desc = new ArrayList<String>();
        date = new ArrayList<String>();

        ((CalendarAdapter) parent.getAdapter()).setSelected(v);
        String selectedGridDate = CalendarAdapter.dayString.get(position);
        String[] separatedTime = selectedGridDate.split("-");
        // taking last part of date. ie; 2 from 2012-12-02.
        String gridvalueString = separatedTime[2].replaceFirst("^0*", "");

        int gridvalue = Integer.parseInt(gridvalueString);

        // navigate to next or previous month on clicking offdays.
        if ((gridvalue > 10) && (position < 8)) {
            setPreviousMonth();
            refreshCalendar();
        } else if ((gridvalue < 7) && (position > 28)) {
            setNextMonth();
            refreshCalendar();
        }
        ((CalendarAdapter) parent.getAdapter()).setSelected(v);

        for (int i = 0; i < Utility.startDates.size(); i++) {
            if (Utility.startDates.get(i).equals(selectedGridDate)) {
                desc.add(Utility.nameOfEvent.get(i));
            }
        }

        if (desc.size() > 0) {
            for (int i = 0; i < desc.size(); i++) {
                // set some properties of rowTextView or something
                rowTextView.setText("Event on: "+selectedGridDate +": " + desc.get(i));

                // final ListView listView = (ListView) view.findViewById(R.id.list);
                quickAction.show(v);
            }
        } else {
            Calendar cal = Calendar.getInstance(); 
            GregorianCalendar calDate = new GregorianCalendar();
            if (Build.VERSION.SDK_INT >= 14) {
                //Assuming the selectedGridDate="11-August-2013";
                long startTime = getMilliSeconds(selectedGridDate) + 1000 * 60 * 60;  
                long endTime = getMilliSeconds(selectedGridDate) + 1000 * 60 * 60 * 2;
                Intent intent = new Intent(Intent.ACTION_INSERT).setData(Events.CONTENT_URI).
                    .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,startTime)
                    .putExtra(CalendarContract.EXTRA_EVENT_END_TIME,endTime)
                    .putExtra(Events.TITLE, "Yoga")
                    .putExtra(Events.DESCRIPTION, "Group class")
                    .putExtra(Events.EVENT_LOCATION, "The gym")
                    .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY)
                    .putExtra(Intent.EXTRA_EMAIL, "owan@example.com,trevor@example.com");
                 startActivity(intent);

             } else {
                 Intent intent = new Intent(Intent.ACTION_INSERT);
                 intent.setType("vnd.android.cursor.item/event");
                 intent.putExtra("beginTime", cal.getTimeInMillis());
                 intent.putExtra("allDay", false);
                 intent.putExtra("rrule", "FREQ=DAILY");
                 intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
                 intent.putExtra("title", "A Test Event from android app");

                 startActivity(intent);
             }
         }
     desc = null;
     }
});

public long getMilliSeconds(String date) throws ParseException {       
    // As above
}