Teams Winter 2011/team1/BlackBerry/Implement Delete Option

From CDOT Wiki
Revision as of 16:02, 21 March 2011 by Azea (talk | contribs)
Jump to: navigation, search

7. Implement Delete Student option

7.1. In the ViewStudentApp implement DeleteCommandHandler by adding anonymous class:

  deleteItem.setCommand(new Command(new CommandHandler(){
       public void execute(ReadOnlyCommandMetadata metadata, Object context) {
         Student student = (Student) _keywordFilterField.getSelectedElement();
         if (student != null) {                 
           String[] selections = {"Yes","Cancel"};
           Dialog deleteDialog = new Dialog("Are you sure you want to delete " + student.getName() + "?", selections, null, 0, null);
                 
           // Display the dialog and deletes the element from the list
           if(deleteDialog.doModal() == 0) {
              _studentList.deleteElement(student);
              Dialog.alert(student.getName() + " has been deleted!");
              }
           }
         }
      }));
 

7.2. Add the deleteElement() method to the StudentList class:

  void deleteElement(Object element) {
        doRemove(element);
    }
 

7.3. Run the application, select one student and select "Delete Student" from the menu:
BB delete1.png
7.4. Click "Delete Student". A dialog box pop-ups asking to confirm the student deletion:
BB delete2.png
7.5. Select "Yes" Option. The dialog window comes up acknowledging that student is deleted:
BB delete3.png
7.6. Add "Delete Student" as option in view screen. Change the one argument constructor of the anonymous class to two argument constructor:

  InfoScreen(final Student student, final StudentList studentList) {  }
 

7.7. Add MenuItem to this screen menu and implement CommandHandler:

  MenuItem deleteItem = new MenuItem(new StringProvider("Delete Student"), 400, 4);
  deleteItem.setCommand(new Command(new CommandHandler(){
      public void execute(ReadOnlyCommandMetadata metadata, Object context) {
                  
      if (student != null) {                 
        String[] selections = {"Yes","Cancel"};
        Dialog deleteDialog = new Dialog("Are you sure you want to delete " + student.getName() + "?", selections, null, 0, null);
                     
        // Display the dialog and deletes the element from the list
        if(deleteDialog.doModal() == 0) {
           studentList.deleteElement(student);
           Dialog.alert(student.getName() + " has been deleted!");
           close();
         }
        }
       }
   }));
   addMenuItem(deleteItem);
 

7.8. Run the application, select a student and inside the View Screen go to the menu and select "Delete Student":
BB delete4.png