4.5.10. deleteSearch Method

The event handler in Fig. 4.25 calls method deleteSearch (Figure 4.27) when the user long presses a search tag and selects Delete. Before deleting the search, the app displays an AlertDialog to confirm the delete operation. Lines 241–242 set the dialog’s title to a formatted String in which tag replaces the format specifier in the String resource R.string.confirmMessage ("Are you sure you want to delete the search "%s"?"). Lines 245–254 configure the dialog’s negative button to dismiss the dialog. The String resource R.string.cancel represents "Cancel". Lines 257–275 configure the dialog’s positive button to remove the search. The String resource R.string.delete represents "Delete". Line 263 removes the tag from the tags collection, and lines 266–269 use a SharedPreferences.Editor to remove the search from the app’s SharedPreferences. Line 272 then notifies the ArrayAdapter that the underlying data has changed so that the ListView can update its displayed list of items.


234      // deletes a search after the user confirms the delete operation
235      private void deleteSearch(final String tag)
236      {
237         // create a new AlertDialog
238         AlertDialog.Builder confirmBuilder = new AlertDialog.Builder(this);
239
240         // set the AlertDialog's message
241         confirmBuilder.setMessage(
242            getString(R.string.confirmMessage, tag));
243
244         // set the AlertDialog's negative Button
245         confirmBuilder.setNegativeButton(getString(R.string.cancel ),
246            new DialogInterface.OnClickListener()
247            {
248               // called when "Cancel" Button is clicked
249               public void onClick(DialogInterface dialog, int id)
250               {
251                  dialog.cancel(); // dismiss dialog
252               }
253            }
254         ); // end call to setNegativeButton
255
256         // set the AlertDialog's positive Button
257         confirmBuilder.setPositiveButton(getString(R.string.delete ),
258            new DialogInterface.OnClickListener()
259            {
260               // called when "Cancel" Button is clicked
261               public void onClick(DialogInterface dialog, int id)
262               {
263                  tags.remove(tag); // remove tag from tags
264
265                  // get SharedPreferences.Editor to remove saved search
266                  SharedPreferences.Editor preferencesEditor =
267                     savedSearches.edit();
268                  preferencesEditor.remove(tag); // remove search
269                  preferencesEditor.apply(); // saves the changes
270
271                  // rebind tags ArrayList to ListView to show updated list
272                  adapter.notifyDataSetChanged();
273               }
274            } // end OnClickListener
275         ); // end call to setPositiveButton
276
277         confirmBuilder.create().show(); // display AlertDialog
278      } // end method deleteSearch
279   } // end class MainActivity


Fig. 4.27 | deleteSearch method of class MainActivity.

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

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