如何从android calendar api获取Event实例?

时间:2014-06-13 09:19:53

标签: android events calendar instance recurring

我正在尝试获取重复事件实例的ID,我正在执行此任务

                                     final String[] INSTANCE_PROJECTION = new String[] {
                                            Instances.EVENT_ID,      // 0
                                            Instances.BEGIN,         // 1
                                            Instances.TITLE          // 2
                                          };
                                    Calendar beginTime = Calendar.getInstance();
                                    beginTime.set(2011, 9, 23, 8, 0);
                                    long startMillis = beginTime.getTimeInMillis();
                                    Calendar endTime = Calendar.getInstance();
                                    endTime.set(2011, 10, 24, 8, 0);
                                    long endMillis = endTime.getTimeInMillis();

                                    Cursor cur = null;
                                    ContentResolver cr = view.getContext().getContentResolver();

                                    String selection = Instances.EVENT_ID + " = ?";
                                    String[] selectionArgs = new String[] {"207"};

                                    Uri.Builder builder = Instances.CONTENT_URI.buildUpon();
                                    ContentUris.appendId(builder, startMillis);
                                    ContentUris.appendId(builder, endMillis);

                                    final int PROJECTION_ID_INDEX = 0;
                                    final int PROJECTION_BEGIN_INDEX = 1;
                                    final int PROJECTION_TITLE_INDEX = 2;

                                    cur =  cr.query(builder.build(), 
                                            INSTANCE_PROJECTION, 
                                            selection, 
                                            selectionArgs, 
                                            null);

                                    while (cur.moveToNext()) {
                                        String title = null;
                                        long eventID = 0;
                                        long beginVal = 0;    

                                        // Get the field values
                                        eventID = cur.getLong(PROJECTION_ID_INDEX);
                                        beginVal = cur.getLong(PROJECTION_BEGIN_INDEX);
                                        title = cur.getString(PROJECTION_TITLE_INDEX);

                                        // Do something with the values. 
                                        //Log.i(DEBUG_TAG, "Event:  " + title); 
                                        Calendar calendar = Calendar.getInstance();
                                        calendar.setTimeInMillis(beginVal);  
                                        SimpleDateFormat formatter  = new SimpleDateFormat("MM/dd/yyyy");
                                        //Log.i(DEBUG_TAG, "Date: " + formatter.format(calendar.getTime()));    
                                        Toast.makeText(view.getContext(), "Date: " + formatter.format(calendar.getTime()), Toast.LENGTH_SHORT).show();

但它不起作用,我不明白我哪里出错了。如果有人遇到问题,请告诉我。

1 个答案:

答案 0 :(得分:2)

我已经成功完成了,现在我能够获得一个实例..这是一个代码:

  final String[] INSTANCE_PROJECTION = new String[] {

                                            Instances.EVENT_ID,      // 0
                                            Instances.BEGIN,         // 1
                                            Instances.TITLE          // 2
                                          };
                                    Calendar beginTime = Calendar.getInstance();
                                    beginTime = selectedAppointmentInstance.getStartDate();
                                    long startMillis = beginTime.getTimeInMillis();
                                    Calendar endTime = Calendar.getInstance();
                                    endTime = selectedAppointmentInstance.getEndDate();
                                    long endMillis = endTime.getTimeInMillis()-1;

                                    Cursor cur = null;
                                    ContentResolver cr = view.getContext().getContentResolver();

                                    String selection = Instances.EVENT_ID + " = ?" ;
                                    String[] selectionArgs = new String[] {selectedAppointmentInstance.getAppointmentId()};

                                    Uri.Builder builder = Instances.CONTENT_URI.buildUpon();
                                    ContentUris.appendId(builder, startMillis);
                                    ContentUris.appendId(builder, endMillis);




                                    String where=Instances.EVENT_ID + " = " + selectedAppointmentInstance.getAppointmentId();
                                     final int PROJECTION_ID_INDEX = 0;
                                    final int PROJECTION_BEGIN_INDEX = 1;
                                    final int PROJECTION_TITLE_INDEX = 2;

                                    cur =  cr.query(builder.build(), 
                                            INSTANCE_PROJECTION, 
                                            selection, 
                                            selectionArgs, 
                                            null);

                                    while (cur.moveToNext()) {
                                        String title = null;
                                        long eventID = 0;
                                        long beginVal = 0;    

                                        // Get the field values
                                        eventID = cur.getLong(PROJECTION_ID_INDEX);
                                        beginVal = cur.getLong(PROJECTION_BEGIN_INDEX);
                                        title = cur.getString(PROJECTION_TITLE_INDEX);

                                        // Do something with the values. 
                                        //Log.i(DEBUG_TAG, "Event:  " + title); 
                                        Calendar calendar = Calendar.getInstance();
                                        calendar.setTimeInMillis(beginVal);  
                                        SimpleDateFormat formatter  = new SimpleDateFormat("MM/dd/yyyy");
                                        //Log.i(DEBUG_TAG, "Date: " + formatter.format(calendar.getTime()));    
                                        Toast.makeText(view.getContext(), "Date: " + formatter.format(calendar.getTime()), Toast.LENGTH_SHORT).show();






                                    }