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

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
}
于 2013-08-19T09:30:06.533 に答える