Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Revision as of 04:35, 26 July 2013 by hamishwillee (Talk | contribs)

Creating a ToDo item and retrieving the ToDo list using JSR-75

From Wiki
Jump to: navigation, search
Article Metadata
Compatibility
Platform(s):
Series 40
Symbian
Article
Created: User:Kbwiki (09 Feb 2011)
Last edited: hamishwillee (26 Jul 2013)

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

File:Create and Retrieve Todo.zip

85 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×