Changes

Jump to: navigation, search

Teams Winter 2011/team1/Android/Edit Contact

4,195 bytes added, 13:36, 7 April 2011
8.1 Create Edit Layout and Menu Item
== 8. Edit Contact ==
===8.1 Create Edit Layoutand Menu Item===8.1.1 Add the String value for the layout title to String.xml:===
<source lang="java">
<string name="editContact_textView">Edit Contact</string>
</LinearLayout>
</source>
8.1.6Now Add the Edit menu Item. for this purpose click on menu.xml and brouse to the xml view, and add the menu item. Here is how it looks like:<source lang="java"><?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/item1" android:title="Add Student"></item> <item android:id="@+id/item2" android:title="Delete Student"></item> <item android:id="@+id/item3" android:title="View Student"></item> <item android:id="@+id/item4" android:title="Edit Student"></item> <item android:id="@+id/item5" android:title="Send E-mail"></item></menu></source>  8.1.. Now build your project so that the ids are generated in R.java
===8.2 Create EditContactActivity===
</source>
===8.4 Implement The the EditContact in the main activity class===8.4.1 Add the following private fields to the ContactList Activity :<source lang=java> // added for Edit private static final int EDIT_CONTACT =1; private Student selectedStudent; </source> 8.4.2 In the onOptionsItemSelected(MenuItem item) method add the following case to the switch code block: <source lang="java">case R.id.item4:// Edit // set the private field :selectedStudent so that it can be accessed later selectedStudent = (Student)contactsList.getSelectedItem(); // create an Intent for EditContactActivity class Intent j = new Intent(this, EditContactActivity.class); // add the selected student's data to the intent so that the EditContactActivity class can read them j.putExtra("oldFirstName",selectedStudent.getFirstname()); j.putExtra("oldLastName", selectedStudent.getLastName()); j.putExtra("oldEmail",selectedStudent.getEmail()); // Start the EditContactActivity activity startActivityForResult(j, EDIT_CONTACT); break; </source>8.4.3 Modify the onActivityResult() method to accommodate the results of the EditContactActivity. Here is the completed code if this method: <source lang="java">@Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) {// modified to implement Edit switch (requestCode){ case CREATE_CONTACT: // Add new Contact if (resultCode == Activity.RESULT_OK) { studentList.addElement(new Student(intent.getExtras().getString("firstName"), intent.getExtras().getString("lastName"), intent.getExtras().getString("email"))); } else { showDialog("No contact was added!"); } displayData(studentList); break; case EDIT_CONTACT: // edit the selected Student // implement Edit if (resultCode == Activity.RESULT_OK) { Student oldS = selectedStudent; Student newS= new Student(intent.getExtras().getString("newFirstName"), intent.getExtras().getString("newLastName"), intent.getExtras().getString("newEmail")); for(int i = 0; i< studentList.size() ; i++){ Student s= (Student)studentList.elementAt(i); if (s.getName().compareTo(oldS.getName())==0 && s.getEmail().compareTo(s.getEmail())==0){ studentList.remove(s); studentList.add(newS); } } } else { showDialog("No contact was edited!"); } displayData(studentList); break; default: break; } } </source>
===8.5 Edit Contact In action ===
8.5.1 Build and run the project. Scrole mous buton to select a contact, and then click on the menu button of the virtual device. and then click on the EditContact menu option. Please note that the selected contact seems unselected but it's ok:<br/>
[[Image: editMenuOption.png | 450px]]<br/>
8.5.2 the Edit Contact view will apear with the selected student shown.<br/>
[[Image: editView.png | 450px]]<br/>
8.5.3 edit the Contact's info:<br/>
[[Image: editViewEdited.png | 450px]]<br/>
8.5.4 click on save contact button and see the result:<br/>
[[Image: editViewSaved.png | 450px]]<br/>
1
edit

Navigation menu