Open main menu

CDOT Wiki β

Teams Winter 2011/team1/Android/Implement Delete Option

5. Implement Delete Option

5.1. In the ContactList class add the following lines to the onOptionsItemSelected method:

   case R.id.item2:
            Student deleteStudent = (Student)contactsList.getSelectedItem();
            if (deleteStudent != null){
            studentList.removeElement(deleteStudent);           
            displayData(studentList);
            } else {
                showDialog("No contact was selected!");               
            }
            break;

5.2. Add the method to generate the dialog window to alert if no contact has been selected:

  public void showDialog(String message){
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setMessage(message);
        alertDialog.show();
    }

5.3. Run the application, select one student from the list, press menu and select "Delete Student":
 
5.4. The list should get updated:
 
5.5. Run the application again, but this time don't select any student and try to execute the delete option. You should get the following message: