Creating a ToDo item and retrieving the ToDo list using JSR-75
Article Metadata
Compatibility
Platform(s): Series 40, S60/Symbian
Article
Keywords: File Connection and PIM API (JSR-75),javax.microedition.pim.PIM, javax.microedition.pim.PIMItem, javax.microedition.pim.ToDo, javax.microedition.pim.ToDoList
Created: User:Kbwiki
(09 Feb 2011)
Last edited: hamishwillee
(14 Jun 2012)
Overview
PIM APIs provide access to Personal Information Management (PIM) data on J2ME devices. PIM data is defined as information included in address book, calendar application, and to-do list applications.
The following code snippet demonstrates the use of ToDo and ToDoList Interfaces to create a new ToDo. This example illustrates how to create a ToDo by specifying its fields and also retrieve the list of ToDos available in the native PIM database.
Source file
Class imports
import javax.microedition.midlet.MIDlet; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.StringItem; import javax.microedition.lcdui.TextField; import javax.microedition.lcdui.DateField; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.Alert; import javax.microedition.pim.PIM; import javax.microedition.pim.PIMItem; import javax.microedition.pim.PIMException; import java.util.Date; import java.util.Enumeration; import javax.microedition.lcdui.Choice; import javax.microedition.lcdui.List; import javax.microedition.pim.ToDo; import javax.microedition.pim.ToDoList;
Application initialization
public class ToDoMiDlet extends MIDlet implements CommandListener {
private Display display;
// Form for entering data of new ToDo.
private Form addToDoForm;
// Command for adding ToDo to list of ToDos
private Command cmdAddTodo;
// Command for retrieving ToDos from list of ToDos.
private Command cmdretrieveTodo;
private Command cmdExit,backCommand;
// Text field for summary of ToDo.
private TextField summaryField;
// Date field for Due data of ToDo.
private DateField dueDateField;
// Date field for end data of ToDo.
private TextField noteField;
// Text field to set priority of ToDo.
private TextField priorityField;
private List listTodos;
private ToDoList todoList;
private ToDo todo;
/**
* Constructor.
*/
public ToDoMiDlet() {
if(checkPIMSupport() == false) {
exitMIDlet();
}
setComponents();
}
/**
* Initializes components of MIDlet.
*/
public void setComponents() {
display = Display.getDisplay(this);
// Create form for adding ToDo.
addToDoForm = new Form("ToDo");
// Add commands to form and set listener for it.
cmdAddTodo = new Command("Add Todo", Command.SCREEN, 0);
cmdretrieveTodo = new Command("Retrieve All Todos", Command.SCREEN, 0);
addToDoForm.addCommand(cmdAddTodo);
addToDoForm.addCommand(cmdretrieveTodo);
cmdExit = new Command("Exit", Command.EXIT, 0);
addToDoForm.addCommand(cmdExit);
addToDoForm.setCommandListener(this);
listTodos = new List("ToDo list", Choice.IMPLICIT);
backCommand =new Command("Back", Command.BACK, 0);
listTodos.addCommand(backCommand);
listTodos.setCommandListener(this);
try {
// ...
// Get list of ToDos.
todoList = (ToDoList)PIM.getInstance().openPIMList(
PIM.TODO_LIST, PIM.READ_WRITE);
// Create controls based on supported fields for ToDo.
if(todoList.isSupportedField(ToDo.SUMMARY) == true) {
summaryField = new TextField("Summary", null, 20,
TextField.ANY);
addToDoForm.append(summaryField);
}
// ...
if(todoList.isSupportedField(ToDo.DUE) == true) {
dueDateField = new DateField("Date",
DateField.DATE_TIME);
dueDateField.setDate(new Date());
addToDoForm.append(dueDateField);
}
// ...
if(todoList.isSupportedField(ToDo.NOTE) == true) {
noteField = new TextField("Information", null, 20, TextField.ANY);
addToDoForm.append(noteField);
}
// ...
if(todoList.isSupportedField(ToDo.PRIORITY) == true) {
priorityField = new TextField("Priority", null, 20,
TextField.NUMERIC);
addToDoForm.append(priorityField);
}
// ...
// ...
}
} catch(PIMException pimExc) {
// TODO: Handle error on working with PIM.
}
catch(SecurityException secExc) {
// TODO: Handle error on access to PIM.
}
catch(Exception exc) {
// If unknown error was caught, show it to the end user.
showAlert("Info", exc.toString());
}
}
Create a ToDo
/**
* Adds ToDo to list of ToDos.
* Gets data for ToDo from addToDoForm controls.
*/
private void addToDo() {
try {
// Get list of ToDos.
todoList = (ToDoList)PIM.getInstance().openPIMList(
PIM.TODO_LIST, PIM.READ_WRITE);
// Create new ToDo.
todo = todoList.createToDo();
// Get data from controls
if(todoList.isSupportedField(ToDo.SUMMARY) == true) {
if(summaryField.size()==0){
showAlert("Info", "Summary missing, please enter a summary");
return;
}
String summary =summaryField.getString();
todo.addString(ToDo.SUMMARY, PIMItem.ATTR_NONE, summary);
} else {
// At least summary must be supported.
closeToDoList();
showAlert("Info", "Summary field for ToDo is not supported");
}
if(todoList.isSupportedField(ToDo.DUE) == true) {
long startDate = dueDateField.getDate().getTime();
todo.addDate(ToDo.DUE, PIMItem.ATTR_NONE, startDate);
}
if(todoList.isSupportedField(ToDo.NOTE) == true) {
String note = noteField.getString();
todo.addString(ToDo.NOTE, PIMItem.ATTR_NONE, note);
}
if(todoList.isSupportedField(ToDo.PRIORITY) == true) {
if(priorityField.size()==0){
showAlert("Info", "Priority is missing, please enter a priority");
return;
}
else {
String location = priorityField.getString();
int i=Integer.parseInt(location);
if(i > 9){
showAlert("Info","Priority exceeded, please enter a priority between 0-9");
return;
}
todo.addInt(ToDo.PRIORITY, PIMItem.ATTR_NONE, i);
}
}
// Commit ToDo.
todo.commit();
// Notify user that ToDo was added
showAlert("Info", "ToDo was successfully added");
} catch(PIMException pimExc) {
// TODO: Handle error on working with PIM.
showAlert("Info", pimExc.getMessage());
}
catch(SecurityException secExc) {
// TODO: Handle error on access to PIM.
showAlert("Info", secExc.getMessage());
}
catch(Exception exc) {
// TODO: Handle all other errors.
showAlert("Info", exc.toString());
}
}
Retrieve the list of ToDos
/**
* Retrieves the list of all ToDos available in database.
*/
public void listToDos() {
Enumeration todos = null;
try {
todoList = (ToDoList) PIM.getInstance().openPIMList(PIM.TODO_LIST, PIM.READ_WRITE);
} catch (PIMException e) {
// Cannot open ToDo list
showAlert("Info", "Failed to open a ToDo list: "+e.toString());
return;
} catch (SecurityException e) {
// User rejects application's request for reading ToDo list
showAlert("Info", "Reading ToDo List rejected: "+e.toString());
return;
}
try {
// Get the enumeration of all ToDo elements
todos = todoList.items();
} catch (PIMException e) {
// Failed to retrieve elements
showAlert("Info", "This application cannot retrieve ToDos: "+e.toString());
}
if (listTodos.size() > 0) {
listTodos.deleteAll();
}
while (todos.hasMoreElements()) {
todo = (ToDo) todos.nextElement();
String todoInfo = null;
try {
todoInfo = todo.getString(ToDo.SUMMARY, PIMItem.ATTR_NONE);
} catch (Exception ex) {
showAlert("Info",ex.getMessage());
continue;
}
if (todoInfo != null) {
listTodos.append(todoInfo, null);
}
}
}
Sample application


(no comments yet)