Figure 4.24 declares and initializes instance variable itemClickListener
, which refers to an anonymous inner-class object that implements interface OnItemClickListener
. Line 67 (Fig. 4.20) registered itemClickListener
as the ListView
’s event handler that responds when the user touches an item in the ListView
. Lines 131–145 override interface OnItemClickListener
’s onItemClick
method. The method’s arguments are:
• The AdapterView
where the user touched an item. The ?
in AdapterView<?>
is a wildcard in Java generics indicating method onItemClick
can receive an AdapterView
that displays any type of data—in this case, a ListView<String>
.
• The View
that the user touched in the AdapterView
—in this case, the TextView
that displays a search tag.
• The zero-based index number of the item the user touched.
• The row ID of the item that was touched—this is used primarily for data obtained from a database (as you’ll do in Chapter 8).
128 // itemClickListener launches web browser to display search results
129 OnItemClickListener itemClickListener = new OnItemClickListener()
130 {
131 @Override
132 public void onItemClick(AdapterView<?> parent, View view,
133 int position, long id)
134 {
135 // get query string and create a URL representing the search
136 String tag = ((TextView) view).getText().toString();
137 String urlString = getString(R.string.searchURL) +
138 Uri.encode(savedSearches.getString(tag, ""), "UTF-8");
139
140 // create an Intent to launch a web browser
141 Intent webIntent = new Intent(Intent.ACTION_VIEW,
142 Uri.parse(urlString));
143
144 startActivity(webIntent); // launches web browser to view results
145 }
146 }; // end itemClickListener declaration
147
Line 136 gets the text of the View
that the user touched in the ListView
. Lines 137–138 create a String
containing the Twitter search URL and the query to perform. First, line 137 calls Activity
’s inherited method getString with one argument to get the String
resource named searchURL
, which contains the Twitter search page’s URL:
As with all the String
resources in this app, you should add this resource to strings.xml
.
We append the result of line 138 to the search URL to complete the urlString
. SharedPreferences
method getString returns the query associated with the tag
. If the tag
does not already exist, the second argument (""
in this case) is returned. Line 138 passes the query to Uri
method encode
, which escapes any special URL characters (such as ?
, /
, :
, etc.) and returns a so-called URL-encoded String
. This is important to ensure that the Twitter web server that receives the request can parse the URL properly to obtain the search query.
Lines 141–142 create a new Intent
, which we’ll use to launch the device’s web browser and display the search results. Intent
s can be used to launch other activities in the same app or in other apps. The first argument of Intent
’s constructor is a constant describing the action to perform. Intent.ACTION_VIEW indicates that we’d like to display a representation of the data. Many constants are defined in the Intent
class describing actions such as searching, choosing, sending and playing. The second argument (line 142) is a Uri (uniform resource identifier) representing the data on which we want to perform the action. Class Uri
’s parse method converts a String
representing a URL (uniform resource locator) to a Uri
.
Line 144 passes the Intent
to the inherited Activity
method startActivity
, which starts an Activity
that can perform the specified action on the given data. In this case, because we’ve specified to view a URI, the Intent
launches the device’s web browser to display the corresponding web page. This page shows the results of the supplied Twitter search.