1

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

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;



        }

任何帮助表示赞赏。

4

1 回答 1

1

选择GridDate值后,将其传递给以下方法。
例子: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
}
于 2013-08-19T09:30:06.533 回答