Changes

Jump to: navigation, search

Teams Winter 2011/team1/Android/Add Contact

1,297 bytes added, 17:26, 27 March 2011
6.4. Implement Add Contact in the main activity class
=== 6.4. Implement Add Contact in the main activity class ===
6.4.1. First step is to start the activity for the ''Add Contact'' when it's selected from the menu items.
<source lang="java">
case R.id.item1:
Intent i = new Intent(this, AddContactActivity.class);
startActivityForResult(i, CREATE_CONTACT); // to process the result that is returned from the AddContactActivity
break;
</source>
 
6.4.2. In ''startActivityForResult()'', two things need to be checked first, ''resultCode'' and the name of the activity, if these two were fine, the new student with the added values will be added to the list of students, otherwise there will be an alert message displayed to the user.
<source lang="java">
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode == Activity.RESULT_OK && requestCode == CREATE_CONTACT) {
studentList.addElement(new Student(intent.getExtras().getString("firstName"),
intent.getExtras().getString("lastName"), intent.getExtras().getString("email")));
}
else {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setMessage("No contact was added!");
alertDialog.show();
}
displayData(studentList);
}
</source>
1
edit

Navigation menu