| ID | CS001393 | Creation date | June 5, 2009 |
| Platform | S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition, S40 3rd Edition FP 1 | Tested on devices | Nokia N80, Nokia N79, Nokia 5800 XpressMusic, Nokia 6131 |
| Category | Java ME | Subcategory | PIM |
| Keywords (APIs, classes, methods, functions): javax.microedition.pim.Event, javax.microedition.pim.EventList,
javax.microedition.pim.PIM, javax.microedition.pim.PIMException |
The following code snippet demonstrates how to delete an event from the calendar using the PIM API.
The list of events can be retrieved using PIM.openPIMList. For more information, refer to the code snippet Listing calendar events in Java ME.
After the event that should be deleted is found, and we have an Event reference, the method EventList.removeEvent is called to delete the event.
In order to test this snippet there should be at least one event in the database. The application also needs permission to read and write PIM database data.
private List listEvents;
/**
* Holds reference to the currently opened event list.
*/
private EventList eventList;
private static final Command DELETE_COMMAND =
new Command("Delete", Command.ITEM, 0);
When an event is selected in the list, delete it:
listEvents.setSelectCommand(DELETE_COMMAND);
listEvents.setCommandListener(this);
/**
* Deletes an event by it's index in the event list.
* @param itemIndex index of the event in the event list.
*/
private void deleteEvent(int itemIndex) {
int i = 0;
Enumeration eventItems = null;
Event event = null;
try {
printString("Getting event list items...");
eventItems = eventList.items();
} catch (PIMException ex) {
releaseEventList();
printString(ex.toString());
return;
}
// Checking event count
if (!eventItems.hasMoreElements()) {
printString("Event list is empty!");
}
event = (Event)eventItems.nextElement();
while ((i != itemIndex) && (eventItems.hasMoreElements())) {
event = (Event)eventItems.nextElement();
i++;
}
if (event == null) {
return;
}
try {
printString("Removing event...");
eventList.removeEvent(event);
} catch (PIMException ex) {
printString(ex.toString());
}
}
/**
* From CommandListener.
* Called by the system to indicate that a command has been invoked on a
* particular displayable.
* @param command the command that was invoked
* @param displayable the displayable where the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {
if (command == DELETE_COMMAND) {
// Get the currently selected event
int i = listEvents.getSelectedIndex();
String item = listEvents.getString(i);
printString("Selected event: " + item);
// Bring the main form back to the foreground
display.setCurrent(mainForm);
deleteEvent(i);
// Close the event list
releaseEventList();
}
}
The selected event is removed from the calendar.
This code snippet is part of the stub concept, which means that it has been patched on top of a template application in order to be more useful to developers. The version of the Java ME stub application used as a template in this snippet is v1.1.
No related wiki articles found