Listing 33 First half of the Review List Activity class showing a List View

public class ReviewList extends ListActivity { <1—Q Extend ListActivity private static final int MENU_CHANGE_CRITERIA = Menu. FIRST + 1 ; private static final int MENU_GET_NEXT_PAGE = Menu. FIRST; private static final int NUM_RESULTS_PER_PAGE = 8;

private TextView empty;

private ProgressDialog progressDialog; Use ReviewAdapter private ReviewAdapter reviewAdapter; <1-'

private List<Review> reviews; <1—Q Back Adapter with List private final Handler handler = new Handler() { - , public void handleMessage ( final Message msg) { ' e Handler progressDialog. dismiss () ; E for messages if ( (reviews == null) | | (reviews . size () == 0) ) {

reviewAdapter = new ReviewAdapter(ReviewList.this, reviews); setListAdapter(reviewAdapter);

@Override public void onCreate(Bundle savedlnstanceState) {

super .onCreate (savedlnstanceState) ; F Use resourced-

this . setContentView (R. layout. review_list) ; <-' defined layout this.empty = (TextView)

findViewByid(R.id.empty); < | Define TextView

ListView listView = getListView () ; G for empty listView.setltemsCanFocus(false);

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); listView.setEmptyView(this.empty);

Set properties for ListView

@Override protected void onResume() { super.onResume () ;

RestaurantFinderApplication application =

(RestaurantFinderApplication) getApplication () ; <1-1

String criteriaCuisine = application.getReviewCriteriaCuisine(); String criteriaLocation = application.getReviewCriteriaLocation();

I Use Application I for global state int startFrom = getIntent().getlntExtra(

Constants . startfrom_extra, 1); <—J Use Intent extra loadReviews(criteriaLocation, criteriaCuisine, startFrom); <1—1) Load review data

/ / onCreateOptionsMenu omitted for brevity

The ReviewList Activity extends ListActivity Q, which is used to host a ListView. The default layout of a ListActivity is a full screen, centered list of choices for the user to select from. A ListView is similar in concept to a Spinner; in fact, they are both subclasses of AdapterView, as you saw in the class diagram in figure 3.4. This means that ListView, like Spinner, also uses an Adapter to bind to data. In this case we are using a custom ReviewAdapter class ©. You will learn more about ReviewAdapter in the next section, when we discuss custom views. The important part here is that we are using an Adapter for our ListView (even though it's a custom adapter), and we use a List of Review objects to populate the Adapter G.

Because we don't yet have the data to populate the list, which we will get from a web service call in another Thread, we need to include a Handler to allow for fetching data and updating the UI to occur in separate steps Q. Don't worry too much about these concepts here, as they will make more sense shortly when we discuss them while looking at the second half of ReviewList in listing 3.4.

After our ListView and its data are declared, we move on to the typical onCreate() tasks we have already seen, including using a layout defined in a resources XML file G- This is significant with respect to ListActivity because a ListView with the ID name "list" is required if you want to customize the layout, as we have done (the ID name is in the layout XML file, which you will see in section 3.3.3). If you don't provide a layout, you can still use ListActivity and ListView; you just get the system default. We are also defining an element that will be used to display the message "No Data" if our List backing our View has no elements G. We also set several specific properties on the ListView, using its customization methods, such as whether or not the list items themselves are focusable, how many elements can be selected at a time, and what View to use when the list is empty Q.

After we set up the View elements needed on the Activity, we get the criteria to make our web service call from the Review object we placed in the Application from the ReviewCriteria Activity G. Here we also use an Intent extra to store a primitive int for page number J. We pass all the criteria data (criteriaLocation, criteria-Cuisine, and startFrom) into the loadReviews() method 1), which eventually makes our web service call to populate our data list. This method, and several others that show how we deal with items in the list being clicked on, are shown in the second half of the ReviewList class, in listing 3.4.

0 0

Post a comment