1
edit
Changes
→8.2 Create EditContactActivity
}
});
</source>
8.3.5 Now you need to override the finish() method:
<source lang="java">
@Override
public void finish() {
Intent i = new Intent();
if (firstName.equals("") && lastName.equals("") && email.equals("")) {
setResult(0);
}
else { // add the modified data to the intent
i.putExtra("newFirstName", firstName);
i.putExtra("newLastName", lastName);
i.putExtra("newEmail", email);
setResult(RESULT_OK, i);
}
super.finish();
}
</source>
8.3.6 Here is the completed source of the EditContactActivity:
<source lang="java">
package cs.ecl.team1.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class EditContactActivity extends Activity {
private String firstName;
private String lastName;
private String email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit);
// get the intent sent to this class
Intent i = this.getIntent();
EditText fNameEditText = (EditText) findViewById(R.id.firstNameEdit);
fNameEditText.setText(i.getStringExtra("oldFirstName"));
EditText lNameEditText = (EditText) findViewById(R.id.lastNameEdit);
lNameEditText.setText(i.getStringExtra("oldLastName"));
EditText emailEditText = (EditText) findViewById(R.id.emailEdit);
emailEditText.setText(i.getStringExtra("oldEmail"));
//save Button implementation
Button saveButton = (Button) findViewById(R.id.saveContact_buttonEdit);
saveButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
firstName = ((EditText) findViewById(R.id.firstNameEdit)).getText().toString();
lastName = ((EditText) findViewById(R.id.lastNameEdit)).getText().toString();
email = ((EditText) findViewById(R.id.emailEdit)).getText().toString();
finish();
}
});
}
@Override
public void finish() {
Intent i = new Intent();
if (firstName.equals("") && lastName.equals("") && email.equals("")) {
setResult(0);
}
else {
i.putExtra("newFirstName", firstName);
i.putExtra("newLastName", lastName);
i.putExtra("newEmail", email);
setResult(RESULT_OK, i);
}
super.finish();
}
}
</source>