Method deleteContact and DialogFragment confirmDelete

Method deleteContact (Fig. 8.50, lines 193–197) displays a DialogFragment (lines 200–252) asking the user to confirm that the currently displayed contact should be deleted. If so, the DialogFragment uses an AsyncTask to delete the contact from the database. If the user clicks the Delete Button in the dialog, lines 222–223 create a new DatabaseConnector. Lines 226–241 create an AsyncTask that, when executed (line 244), passes a Long value representing the contact’s row ID to the doInBackground, which then deletes the contact. Line 232 calls the DatabaseConnector’s deleteContact method to perform the actual deletion. When the doInBackground completes execution, line 239 calls the listener’s onContactDeleted method so that MainActivity can remove the DetailsFragment from the screen.


192   // delete a contact
193   private void deleteContact()
194   {
195      // use FragmentManager to display the confirmDelete DialogFragment
196      confirmDelete.show(getFragmentManager(), "confirm delete");
197   }
198
199      // DialogFragment to confirm deletion of contact
200      private DialogFragment confirmDelete =
201         new DialogFragment()
202         {
203         // create an AlertDialog and return it
204         @Override
205         public Dialog onCreateDialog(Bundle bundle)
206         {
207            // create a new AlertDialog Builder
208            AlertDialog.Builder builder =
209               new AlertDialog.Builder(getActivity());
210
211            builder.setTitle(R.string.confirm_title);
212            builder.setMessage(R.string.confirm_message);
213
214            // provide an OK button that simply dismisses the dialog
215            builder.setPositiveButton(R.string.button_delete,
216               new DialogInterface.OnClickListener()
217               {
218                  @Override
219                  public void onClick(
220                     DialogInterface dialog, int button)
221                  {
222                     final DatabaseConnector databaseConnector =
223                        new DatabaseConnector(getActivity());
224
225                     // AsyncTask deletes contact and notifies listener
226                     AsyncTask<Long, Object, Object> deleteTask =
227                        new AsyncTask<Long, Object, Object>()
228                        {
229                           @Override
230                           protected Object doInBackground(Long... params)
231                           {
232                              databaseConnector.deleteContact(params[0]);
233                              return null;
234                           }
235
236                           @Override
237                           protected void onPostExecute(Object result)
238                           {
239                              listener.onContactDeleted();
240                           }
241                        }; // end new AsyncTask
242
243                     // execute the AsyncTask to delete contact at rowID
244                   deleteTask.execute(new Long[] { rowID });
245                  } // end method onClick
246               } // end anonymous inner class
247            ); // end call to method setPositiveButton
248
249            builder.setNegativeButton(R.string.button_cancel, null);
250            return builder.create(); // return the AlertDialog
251          }
252       }; // end DialogFragment anonymous inner class
253 } // end class DetailsFragment


Fig. 8.50 | Method deleteContact and DialogFragment confirmDelete.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset