When the user touches the Save Contact Button
, the saveContactButtonClicked
listener (Fig. 8.39) executes. To save a contact, the user must enter at least the contact’s name. Method onClick
ensures that the length of the name is greater than 0 characters (line 107) and, if so, creates and executes an AsyncTask
(to perform the save operation). Method doInBackground
(lines 113–118) calls saveContact
(Fig. 8.40) to save the contact into the database. Method onPostExecute
(lines 120–131) programmatically hides the keyboard (lines 124–128), then notifies MainActivity
that a contact was saved (line 130). If the nameEditText
is empty, lines 139–153 display a DialogFragment
telling the user that a contact name must be provided to save the contact.
158 // saves contact information to the database
159 private void saveContact()
160 {
161 // get DatabaseConnector to interact with the SQLite database
162 DatabaseConnector databaseConnector =
163 new DatabaseConnector(getActivity());
164
165 if (contactInfoBundle == null)
166 {
167 // insert the contact information into the database
168 rowID = databaseConnector.insertContact(
169 nameEditText.getText().toString(),
170 phoneEditText.getText().toString(),
171 emailEditText.getText().toString(),
172 streetEditText.getText().toString(),
173 cityEditText.getText().toString(),
174 stateEditText.getText().toString(),
175 zipEditText.getText().toString());
176 }
177 else
178 {
179 databaseConnector.updateContact(rowID,
180 nameEditText.getText().toString(),
181 phoneEditText.getText().toString(),
182 emailEditText.getText().toString(),
183 streetEditText.getText().toString(),
184 cityEditText.getText().toString(),
185 stateEditText.getText().toString(),
186 zipEditText.getText().toString());
187 }
188 } // end method saveContact
189 } // end class AddEditFragment