Possible Errata, listing 31.3


#1

The onCreateView() method described in 31.1 (reproduced in snippet 1 below) throws the following errors.

- The method setOnItemClickListener(AdapterView.OnItemClickListener) in the type AdapterView is not applicable for
the arguments (new OnItemClickListener(){})
- OnItemClickListener cannot be resolved to a type

(both related to the line declaring setOnItemClickListener() method)

and
- The method setOnItemClickListener(AdapterView.OnItemClickListener) in the type AdapterView is not applicable for
the arguments (new OnItemClickListener(){})
- OnItemClickListener cannot be resolved to a type

related to declaring onItemClick()

I was able to fix this, and verify correct behavior, by specifying GridView as the parent class of the onItemClickListener() method (reproduced in snippet 2 below). Am I doing something wrong somewhere else to force this change, or is this an error in the sample code? For completeness, I’ve included my entire PhotoGalleryFragment.java code below (see snippet 3). I have SDK 23.02 installed, and am targeting 19 for the build (didn’t want to get ahead of the book… it throws a warning that I ignore). Using Eclipse 4.2.1

SNIPPET 1

[code]@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_photo_gallery, container, false);

	mGridView = (GridView)v.findViewById(R.id.gridView);
	
	setupAdapter();
	
	mGridView.setOnItemClickListener(new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> gridView, View view, int pos,
				long id) {
			GalleryItem item = mItems.get(pos);
			
			Uri photoPageUri = Uri.parse(item.getPhotoPageUrl());
			Intent i = new Intent(Intent.ACTION_VIEW, photoPageUri);
			
			startActivity(i);
		}
	});

	return v;
}[/code]

SNIPPET 2

[code] @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_photo_gallery, container, false);

	mGridView = (GridView)v.findViewById(R.id.gridView);
	
	setupAdapter();
	
	mGridView.setOnItemClickListener(new GridView.OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> gridView, View view, int pos,
				long id) {
			GalleryItem item = mItems.get(pos);
			
			Uri photoPageUri = Uri.parse(item.getPhotoPageUrl());
			Intent i = new Intent(Intent.ACTION_VIEW, photoPageUri);
			
			startActivity(i);
		}
	});

	return v;
}[/code]

SNIPPET 3

[code]package com.bignerdranch.android.photogallery;

import java.util.ArrayList;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SearchView;

public class PhotoGalleryFragment extends VisibleFragment {
private static final String TAG = “PhotoGalleryFragment”;

GridView mGridView;
ArrayList<GalleryItem> mItems;
ThumbnailDownloader<ImageView> mThumbnailThread;

@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	
	setRetainInstance(true);
	setHasOptionsMenu(true);
	
	// new FetchItemsTask().execute();
	updateItems();
	
	//Intent i = new Intent(getActivity(), PollService.class); // removed on page 474
	//getActivity().startService(i); //removed on page 474
	//PollService.setServiceAlarm(getActivity(), true); //removed on page 476
	
	mThumbnailThread = new ThumbnailDownloader<ImageView>(new Handler());
	mThumbnailThread.setListener(new ThumbnailDownloader.Listener<ImageView>() {
		public void onThumbnailDownloaded(ImageView imageView, Bitmap thumbnail) {
			if (isVisible()) {
				imageView.setImageBitmap(thumbnail);
			}
		}
	});
	mThumbnailThread.start();
	mThumbnailThread.getLooper();
	Log.i(TAG, "background thread started");
}

public void updateItems() {
	new FetchItemsTask().execute();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	View v = inflater.inflate(R.layout.fragment_photo_gallery, container, false);
	
	mGridView = (GridView)v.findViewById(R.id.gridView);
	
	setupAdapter();
	
	mGridView.setOnItemClickListener(new GridView.OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> gridView, View view, int pos,
				long id) {
			GalleryItem item = mItems.get(pos);
			
			Uri photoPageUri = Uri.parse(item.getPhotoPageUrl());
			Intent i = new Intent(Intent.ACTION_VIEW, photoPageUri);
			
			startActivity(i);
		}
	});

	return v;
}

@Override
public void onDestroy() {
	super.onDestroy();
	mThumbnailThread.quit();
	Log.i(TAG, "Background thread destroyed");
}

@Override
@TargetApi(11)
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
	super.onCreateOptionsMenu(menu, inflater);
	inflater.inflate(R.menu.fragment_photo_gallery, menu);
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
		//book says, pull out the search view
		MenuItem searchItem = menu.findItem(R.id.menu_item_search);
		SearchView searchView = (SearchView)searchItem.getActionView();
		
		//book says, get the data from searchable.xml (created earlier) as a SearchableInfo
		SearchManager searchManager = (SearchManager)getActivity()
				.getSystemService(Context.SEARCH_SERVICE);
				ComponentName name = getActivity().getComponentName();
		SearchableInfo searchInfo = searchManager.getSearchableInfo(name);
		
		searchView.setSearchableInfo(searchInfo);
	}
}

@Override
@TargetApi(11)
public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
	case R.id.menu_item_search:
		getActivity().onSearchRequested();
	case R.id.menu_item_clear:
		PreferenceManager.getDefaultSharedPreferences(getActivity())
		.edit()
		.putString(FlickrFetchr.PREF_SEARCH_QUERY, null)
		.commit();
		updateItems();
		return true;
	case R.id.menu_item_toggle_polling:
		boolean shouldStartAlarm = !PollService.isServiceAlarmOn(getActivity());
		PollService.setServiceAlarm(getActivity(), shouldStartAlarm);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
			getActivity().invalidateOptionsMenu();
		return true;
	default:
		return super.onOptionsItemSelected(item);
	}
}

void setupAdapter() {
	if (getActivity() == null || mGridView == null) return;
	
	if (mItems != null) {
		mGridView.setAdapter(new GalleryItemAdapter(mItems));
	} else {
		mGridView.setAdapter(null);
	}
}

private class GalleryItemAdapter extends ArrayAdapter<GalleryItem> {
	public GalleryItemAdapter(ArrayList<GalleryItem> items) {
		super(getActivity(), 0 , items);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = getActivity().getLayoutInflater()
					.inflate(R.layout.gallery_item,  parent, false);
		}
		
		ImageView imageView = (ImageView)convertView
				.findViewById(R.id.gallery_item_imageView);
		imageView.setImageResource(R.drawable.brian_up_close);
		GalleryItem item = getItem(position);
		mThumbnailThread.queueThumbnail(imageView, item.getUrl());
		
		return convertView;
	}
}

private class FetchItemsTask extends AsyncTask<Void,Void,ArrayList<GalleryItem>> {
	@Override
	protected ArrayList<GalleryItem> doInBackground(Void... params) {
		Activity activity = getActivity();
		if (activity == null)
			return new ArrayList<GalleryItem>();
		
		String query = PreferenceManager.getDefaultSharedPreferences(activity)
				.getString(FlickrFetchr.PREF_SEARCH_QUERY, null);
		
		if (query != null) {
			return new FlickrFetchr().search(query);
		} else {
		return new FlickrFetchr().fetchItems();
		}
	}
	@Override
	protected void onPostExecute(ArrayList<GalleryItem> items) {
		mItems = items;
		setupAdapter();
	}
}

@Override
public void onDestroyView() {
	super.onDestroyView();
	mThumbnailThread.clearQueue();
}

@Override
public void onPrepareOptionsMenu(Menu menu) {
	super.onPrepareOptionsMenu(menu);
	
	MenuItem toggleItem = menu.findItem(R.id.menu_item_toggle_polling);
	if (PollService.isServiceAlarmOn(getActivity())) {
		toggleItem.setTitle(R.string.stop_polling);
	} else {
		toggleItem.setTitle(R.string.start_polling);
	}
}

}
[/code]