You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android应用中更新自定义日历的多位参会人员?

Update Multiple Attendees for an Android Calendar Event

Let’s break down two common scenarios you might need: adding multiple new attendees to an event, and updating existing attendees in bulk. Here’s how to implement both:

1. Adding Multiple New Attendees

Instead of inserting one attendee at a time, you can organize your attendee data into a list and loop through it to add each person to the event. Here’s a modified version of your original code scaled for multiple attendees:

// Define a helper class to hold attendee details (keep this in your activity/fragment)
static class Attendee {
    private String name;
    private String email;
    private int type;

    public Attendee(String name, String email, int type) {
        this.name = name;
        this.email = email;
        this.type = type;
    }

    public String getName() { return name; }
    public String getEmail() { return email; }
    public int getType() { return type; }
}

// In your main code:
ContentResolver cr = getContentResolver();
long eventID = 12345; // Replace with your actual event ID

// Create a list of attendees (customize this with your real data)
List<Attendee> attendees = Arrays.asList(
    new Attendee("Alice Smith", "alice@example.com", CalendarContract.Attendees.TYPE_REQUIRED),
    new Attendee("Bob Johnson", "bob@example.com", CalendarContract.Attendees.TYPE_OPTIONAL),
    new Attendee("Charlie Brown", "charlie@example.com", CalendarContract.Attendees.TYPE_REQUIRED)
);

// Loop through each attendee and insert them into the calendar
for (Attendee attendee : attendees) {
    ContentValues attValues = new ContentValues();
    attValues.put(CalendarContract.Attendees.ATTENDEE_NAME, attendee.getName());
    attValues.put(CalendarContract.Attendees.ATTENDEE_EMAIL, attendee.getEmail());
    attValues.put(CalendarContract.Attendees.ATTENDEE_RELATIONSHIP, CalendarContract.Attendees.RELATIONSHIP_NONE);
    attValues.put(CalendarContract.Attendees.ATTENDEE_TYPE, attendee.getType());
    attValues.put(CalendarContract.Attendees.ATTENDEE_STATUS, CalendarContract.Attendees.ATTENDEE_STATUS_INVITED);
    attValues.put(CalendarContract.Attendees.EVENT_ID, eventID);
    
    // Insert the attendee
    Uri uri = cr.insert(CalendarContract.Attendees.CONTENT_URI, attValues);
    
    // Optional: Verify insertion success
    if (uri != null) {
        Log.d("Calendar", "Successfully added attendee: " + attendee.getName());
    } else {
        Log.e("Calendar", "Failed to add attendee: " + attendee.getName());
    }
}

Key Notes:

  • Ensure you have the required permissions: READ_CALENDAR and WRITE_CALENDAR (for Android 10 and below; for Android 11+, use WRITE_CALENDAR and handle runtime permissions properly).
  • Replace eventID with the actual ID of your target calendar event.
  • The Attendee helper class is just a convenient way to organize data—you can use a HashMap or any structure that works for your app.

2. Updating Existing Multiple Attendees

If you need to modify existing attendees (e.g., update their RSVP status or attendance type), first query the event’s current attendees, then loop through them to apply updates. Here’s how:

ContentResolver cr = getContentResolver();
long eventID = 12345; // Replace with your event ID

// Query existing attendees for the event
String[] projection = {
    CalendarContract.Attendees._ID,
    CalendarContract.Attendees.ATTENDEE_EMAIL,
    CalendarContract.Attendees.ATTENDEE_STATUS
};
String selection = CalendarContract.Attendees.EVENT_ID + " = ?";
String[] selectionArgs = {String.valueOf(eventID)};

Cursor cursor = cr.query(
    CalendarContract.Attendees.CONTENT_URI,
    projection,
    selection,
    selectionArgs,
    null
);

if (cursor != null && cursor.moveToFirst()) {
    do {
        long attendeeId = cursor.getLong(cursor.getColumnIndexOrThrow(CalendarContract.Attendees._ID));
        String attendeeEmail = cursor.getString(cursor.getColumnIndexOrThrow(CalendarContract.Attendees.ATTENDEE_EMAIL));
        
        // Example: Update status to ACCEPTED for specific attendees, TENTATIVE for others
        ContentValues updateValues = new ContentValues();
        if ("alice@example.com".equals(attendeeEmail)) {
            updateValues.put(CalendarContract.Attendees.ATTENDEE_STATUS, CalendarContract.Attendees.ATTENDEE_STATUS_ACCEPTED);
        } else {
            updateValues.put(CalendarContract.Attendees.ATTENDEE_STATUS, CalendarContract.Attendees.ATTENDEE_STATUS_TENTATIVE);
        }
        
        // Perform the update
        int rowsUpdated = cr.update(
            ContentUris.withAppendedId(CalendarContract.Attendees.CONTENT_URI, attendeeId),
            updateValues,
            null,
            null
        );
        
        if (rowsUpdated > 0) {
            Log.d("Calendar", "Updated attendee: " + attendeeEmail);
        }
    } while (cursor.moveToNext());
    
    // Always close the cursor to avoid memory leaks
    cursor.close();
}

Key Notes:

  • Customize the update logic to fit your needs (e.g., change attendee type, name, or any other field).
  • For bulk updates targeting multiple attendees at once, you can use ContentResolver.bulkUpdate() with a selection that matches your desired attendees.

内容的提问来源于stack exchange,提问作者Malar Vizhi Rathinasamy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:11:46