Changes

Jump to: navigation, search

Teams Winter 2011/team1/Android/Edit Contact

2,021 bytes added, 12:56, 7 April 2011
8.4 Implement The EditContact in the main activity class
</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>
<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 Intentfor 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
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:
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>
1
edit

Navigation menu