Figure 4.25 declares and initializes instance variable itemLongClickListener
, which refers to an anonymous inner-class object that implements interface OnItemLongClickListener
. Line 70 (Fig. 4.20) registered itemLongClickListener
as the ListView
’s event handler that responds when the user long presses an item in the ListView
. Lines 153–210 override interface OnItemLongClickListener
’s onItemLongClick
method.
148 // itemLongClickListener displays a dialog allowing the user to delete
149 // or edit a saved search
150 OnItemLongClickListener itemLongClickListener =
151 new OnItemLongClickListener()
152 {
153 @Override
154 public boolean onItemLongClick(AdapterView<?> parent, View view,
155 int position, long id)
156 {
157 // get the tag that the user long touched
158 final String tag = ((TextView) view).getText().toString();
159
160 // create a new AlertDialog
161 AlertDialog.Builder builder =
162 new AlertDialog.Builder(MainActivity.this);
163
164 // set the AlertDialog's title
165 builder.setTitle(
166 getString(R.string.shareEditDeleteTitle, tag));
167
168 // set list of items to display in dialog
169 builder.setItems(R.array.dialog_items,
170 new DialogInterface.OnClickListener()
171 {
172 // responds to user touch by sharing, editing or
173 // deleting a saved search
174 @Override
175 public void onClick(DialogInterface dialog, int which)
176 {
177 switch (which)
178 {
179 case 0: // share
180 shareSearch(tag);
181 break ;
182 case 1: // edit
183 // set EditTexts to match chosen tag and query
184 tagEditText.setText(tag);
185 queryEditText.setText(
186 savedSearches.getString(tag, "" ));
187 break ;
188 case 2: // delete
189 deleteSearch(tag);
190 break ;
191 }
192 }
193 } // end DialogInterface.OnClickListener
194 ); // end call to builder.setItems
195
196 // set the AlertDialog's negative Button
197 builder.setNegativeButton(getString(R.string.cancel),
198 new DialogInterface.OnClickListener()
199 {
200 // called when the "Cancel" Button is clicked
201 public void onClick(DialogInterface dialog, int id)
202 {
203 dialog.cancel(); // dismiss the AlertDialog
204 }
205 }
206 ); // end call to setNegativeButton
207
208 builder.create().show(); // display the AlertDialog
209 return true;
210 } // end method onItemLongClick
211 }; // end OnItemLongClickListener declaration
212
Line 158 gets the text of the item the user long pressed and assigns it to final
local variable tag
. Any local variable or method parameter that will be used in an anonymous inner class must be declared final
.
Lines 161–166 create an AlertDialog.Builder
and set the dialog’s title to a formatted String
in which tag
replaces the format specifier in the resource R.string.shareEditDeleteTitle
(which represents "Share, Edit or Delete the search tagged as "%s""
). Line 166 calls Activity
’s inherited method getString
that receives multiple arguments—this first is a String
resource ID representing a format String and the remaining arguments are the values that should replace the format specifiers in the format String
. In addition to buttons, an AlertDialog
can display a list of items in a ListView
. Lines 169–194 specify that the dialog should display the array of String
s R.array.dialog_items
(which represents the String
s "Share"
, "Edit"
and "Delete"
) and define an anonymous inner class to respond when the user touches an item in the list.
The array of String
s is defined as a String
array resource in the strings.xml
file. To add a String
array resource to strings.xml
:
1. Follow the steps in Section 4.5.5 to add a String
resource, but select String Array rather than String in the dialog of Fig. 4.22, then click OK.
2. Specify the array’s name (dialog_items
) in the Name textfield.
3. Select the array in the list of resources at the left side of the resource editor.
4. Click Add...
then click OK
to add a new Item to the array.
5. Specify the new Item’s value in the Value textfield.
Perform these steps for the items Share
, Edit
and Delete
(in that order), then save the strings.xml
file.
The anonymous inner class in lines 170–193 determines which item the user selected in the dialog’s list and performs the appropriate action. If the user selects Share, shareSearch
is called (line 180). If the user selects Edit, lines 184–186 display the search’s query and tag in the EditText
s. If the user selects Delete, deleteSearch
is called (line 189).
Lines 197–206 configure the dialog’s negative button to dismiss the dialog if the user decides not to share, edit or delete the search. Line 208 creates and shows the dialog.