开发者

How to get Calendar Column names from DataBase in android 2.1..?

开发者 https://www.devze.com 2023-03-14 04:12 出处:网络
here is my code.. which works fine in android 1.6.. but throws IllegalArguementException in 2.1 devices.

here is my code.. which works fine in android 1.6.. but throws IllegalArguementException in 2.1 devices.

Uri calendars = Uri.parse("content://com.android.calendar/calendars");
        Cursor managedCursor =   managedQuery(calendars, null,null, null, null);


    if (managedCursor.moveToFirst()) 
    {   int i = managedCursor.getColumnCount();
    for (int j = 0; j &开发者_JAVA百科lt; managedCursor.getColumnCount(); j++) {

        Log.d("#$$$$$$$$############","##"+managedCursor.getColumnName(j));

    }
        Log.d("################@@@@@@@@",String.valueOf(i));

        int nameColumn = managedCursor.getColumnIndex("name");  
        int idColumn = managedCursor.getColumnIndex("_id"); 
        do 
        {    
            calName = managedCursor.getString(1);    
            calId = managedCursor.getString(0); 
            } while (managedCursor.moveToNext());
    }
    ContentValues event = new ContentValues();
    event.put("calendar_id", calId);
    event.put("title", "Event Title");
    event.put("description", "Event Desc");
    event.put("eventLocation", "Event Location");

    event.put("hasAlarm", 1); 
     Uri eventsUri = Uri.parse("content://com.android.calendar/calendars");  
     Uri url = getContentResolver().insert(eventsUri, event);

and the logCat has this...

06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##_id
06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##_sync_account
06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##_sync_account_type
06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##_sync_id
06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##_sync_version
06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##_sync_time
06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##_sync_local_id
06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##_sync_dirty
06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##_sync_mark
06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##url
06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##name
06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##displayName
06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##hidden
06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##color
06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##access_level
06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##selected
06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##sync_events
06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##location
06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##timezone
06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##ownerAccount
06-21 15:53:42.112: DEBUG/#$$$$$$$$############(4605): ##organizerCanRespond
06-21 15:53:42.122: DEBUG/################@@@@@@@@(4605): 21
06-21 15:53:42.132: ERROR/DatabaseUtils(1717): Writing exception to parcel
06-21 15:53:42.132: ERROR/DatabaseUtils(1717): java.lang.IllegalArgumentException: column 'calendar_id' is invalid
06-21 15:53:42.132: ERROR/DatabaseUtils(1717):     at android.database.DatabaseUtils$InsertHelper.getColumnIndex(DatabaseUtils.java:907)
06-21 15:53:42.132: ERROR/DatabaseUtils(1717):     at android.database.DatabaseUtils$InsertHelper.insertInternal(DatabaseUtils.java:883)
06-21 15:53:42.132: ERROR/DatabaseUtils(1717):     at android.database.DatabaseUtils$InsertHelper.insert(DatabaseUtils.java:1011)
06-21 15:53:42.132: ERROR/DatabaseUtils(1717):     at com.android.providers.calendar.CalendarDatabaseHelper.calendarsInsert(CalendarDatabaseHelper.java:88)
06-21 15:53:42.132: ERROR/DatabaseUtils(1717):     at com.android.providers.calendar.CalendarProvider2.insertInTransaction(CalendarProvider2.java:1659)
06-21 15:53:42.132: ERROR/DatabaseUtils(1717):     at com.android.providers.calendar.SQLiteContentProvider.insert(SQLiteContentProvider.java:90)
06-21 15:53:42.132: ERROR/DatabaseUtils(1717):     at android.content.ContentProvider$Transport.insert(ContentProvider.java:174)
06-21 15:53:42.132: ERROR/DatabaseUtils(1717):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:146)
06-21 15:53:42.132: ERROR/DatabaseUtils(1717):     at android.os.Binder.execTransact(Binder.java:288)
06-21 15:53:42.132: ERROR/DatabaseUtils(1717):     at dalvik.system.NativeStart.run(Native Method)
06-21 15:53:42.142: WARN/dalvikvm(4605): threadid=1: thread exiting with uncaught exception (group=0x40020ac0)
06-21 15:53:42.142: ERROR/AndroidRuntime(4605): FATAL EXCEPTION: main
06-21 15:53:42.142: ERROR/AndroidRuntime(4605): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aquilonis.xblockr.callHandller/com.android.pro.utility.AppointmentPro}: java.lang.IllegalArgumentException: column 'calendar_id' is invalid
06-21 15:53:42.142: ERROR/AndroidRuntime(4605):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-21 15:53:42.142: ERROR/AndroidRuntime(4605):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-21 15:53:42.142: ERROR/AndroidRuntime(4605):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-21 15:53:42.142: ERROR/AndroidRuntime(4605):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-21 15:53:42.142: ERROR/AndroidRuntime(4605):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 15:53:42.142: ERROR/AndroidRuntime(4605):     at android.os.Looper.loop(Looper.java:123)
06-21 15:53:42.142: ERROR/AndroidRuntime(4605):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-21 15:53:42.142: ERROR/AndroidRuntime(4605):     at java.lang.reflect.Method.invokeNative(Native Method)
06-21 15:53:42.142: ERROR/AndroidRuntime(4605):     at java.lang.reflect.Method.invoke(Method.java:521)
06-21 15:53:42.142: ERROR/AndroidRuntime(4605):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-21 15:53:42.142: ERROR/AndroidRuntime(4605):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-21 15:53:42.142: ERROR/AndroidRuntime(4605):     at dalvik.system.NativeStart.main(Native Method)
06-21 15:53:42.142: ERROR/AndroidRuntime(4605): Caused by: java.lang.IllegalArgumentException: column 'calendar_id' is invalid
06-21 15:53:42.142: ERROR/AndroidRuntime(4605):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:144)
06-21 15:53:42.142: ERROR/AndroidRuntime(4605):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
06-21 15:53:42.142: ERROR/AndroidRuntime(4605):     at android.content.ContentProviderProxy.insert(ContentProviderNative.java:408)
06-21 15:53:42.142: ERROR/AndroidRuntime(4605):     at android.content.ContentResolver.insert(ContentResolver.java:587)
06-21 15:53:42.142: ERROR/AndroidRuntime(4605):     at com.android.pro.utility.AppointmentPro.onCreate(AppointmentPro.java:54)
06-21 15:53:42.142: ERROR/AndroidRuntime(4605):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-21 15:53:42.142: ERROR/AndroidRuntime(4605):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-21 15:53:42.142: ERROR/AndroidRuntime(4605):     ... 11 more
06-21 15:53:42.152: WARN/ActivityManager(1374):   Force finishing activity com.aquilonis.xblockr.callHandller/com.android.pro.utility.AppointmentPro
06-21 15:53:42.152: WARN/ActivityManager(1374):   Force finishing activity com.aquilonis.xblockr.callHandller/com.android.pro.ui.ProHomeScreen

Is there anyone who can help me out... I'm struggling from past 3 days..

Thanks in advance..


Use the following code for android2.1

Cursor cursor = getContentResolver().query(Uri.parse("content://calendar/calendars"),
                        new String[] { "_id", "displayName" }, "selected=1", null, null);
        if (cursor != null && cursor.moveToFirst()) {
             String[] calNames = new String[cursor.getCount()];
             int[] calIds = new int[cursor.getCount()];
             for (int i = 0; i < calNames.length; i++) {
                  // retrieve the calendar names and ids
                  // at this stage you can print out the display names to get an idea of what calendars the user has
                  calIds[i] = cursor.getInt(0);
                  calNames[i] = cursor.getString(1);
                  cursor.moveToNext();
              }
              cursor.close();
              if (calIds.length > 0) {
                   // we're safe here to do any further work
              }


     // grab calendar id from above
        int cal_id = calIds[0];

        // set the content value
        ContentValues cv = new ContentValues();

        // make sure you add it to the right calendar
        cv.put("calendar_id", cal_id);

        // set the title of the event
        cv.put("title", "jwei's birthday");

        // set the description of the event
        cv.put("description", "Time to celebrate jwei's birthday.");

        // set the event's physical location
        cv.put("eventLocation", "Palo Alto, CA");

        // set the start and end time
        // note: you're going to need to convert the desired date into milliseconds
        cv.put("dtstart", System.currentTimeMillis());
        cv.put("dtend", System.currentTimeMillis() + DateUtils.DAY_IN_MILLIS);

        // let the calendar know whether this event goes on all day or not
        // true = 1, false = 0
        cv.put("allDay", 1);

        // let the calendar know whether an alarm should go off for this event
        cv.put("hasAlarm", 1);

        // once desired fields are set, insert it into the table
        getContentResolver().insert(Uri.parse("content://calendar/events"), cv);
        }


because on line event.put("calendar_id", calId); callId should be int

i have these code for that

private void addToCalendar(Context ctx, final String title, final long dtstart, final long dtend) {
    final ContentResolver cr = ctx.getContentResolver();
    Cursor cursor ;
    if (Integer.parseInt(Build.VERSION.SDK) == 8 )
        cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
    else
        cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
    if ( cursor.moveToFirst() ) {
        final String[] calNames = new String[cursor.getCount()];
        final int[] calIds = new int[cursor.getCount()];
        for (int i = 0; i < calNames.length; i++) {
            calIds[i] = cursor.getInt(0);
            calNames[i] = cursor.getString(1);
            cursor.moveToNext();
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setSingleChoiceItems(calNames, -1, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                ContentValues cv = new ContentValues();
                cv.put("calendar_id", calIds[which]);
                cv.put("title", title);
                cv.put("dtstart", dtstart );
                cv.put("hasAlarm", 1);
                cv.put("dtend", dtstart);


instead of "displayName" use "calendar_displayName"

Now check and let us know..

happy coding!

0

精彩评论

暂无评论...
验证码 换一张
取 消