Difference between revisions of "Teams Winter 2011/team1/Android/Implement Delete Option"

From CDOT Wiki
Jump to: navigation, search
(Created page with '=== 5. Implement Delete Option === 5.1. In the <code>ContactList</code> class add the following lines to the <code>onOptionsItemSelected</code> method: <pre> case R.id.item2:…')
 
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
=== 5. Implement Delete Option ===
 
=== 5. Implement Delete Option ===
 
5.1. In the <code>ContactList</code> class add the following lines to the <code>onOptionsItemSelected</code> method:
 
5.1. In the <code>ContactList</code> class add the following lines to the <code>onOptionsItemSelected</code> method:
  <pre>
+
  <source lang="java">
   case R.id.item2:          
+
   case R.id.item2:
             studentList.removeElement((Student)contactsList.getSelectedItem());           
+
             Student deleteStudent = (Student)contactsList.getSelectedItem();
             displayData();
+
            if (deleteStudent != null){
 +
            studentList.removeElement(deleteStudent);           
 +
             displayData(studentList);
 +
            } else {
 +
                showDialog("No contact was selected!");             
 +
            }
 
             break;
 
             break;
  </pre>
+
  </source>
5.2. Run the application, select one student from the list, press menu and select "Delete Student":<br/>
+
5.2. Add the method to generate the dialog window to alert if no contact has been selected:
 +
<source lang="java">
 +
  public void showDialog(String message){
 +
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
 +
        alertDialog.setMessage(message);
 +
        alertDialog.show();
 +
    }
 +
</source>
 +
5.3. Run the application, select one student from the list, press menu and select "Delete Student":<br/>
 
[[Image: A_delete1.png | 450px]]<br/>
 
[[Image: A_delete1.png | 450px]]<br/>
5.3. The list should get updated: <br/>
+
5.4. The list should get updated: <br/>
[[Image: A_delete2.png | 450px ]]
+
[[Image: A_delete2.png | 450px ]]<br/>
 +
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:<br/>
 +
[[Image: A_view2.png | 450px]]

Latest revision as of 11:19, 28 March 2011

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":
A delete1.png
5.4. The list should get updated:
A delete2.png
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:
A view2.png