Listing 23.4


#1

In the errata, phillips writes:

More About Intents and Tasks, Listing 23.4, the reference to “pm” in the line:

tv.setText(ri.loadLabel(pm));

…will not work, because the “pm” variable is neither an instance variable, nor is it final.

The listing can be fixed by adding a line inside getView() that retrieves the PackageManager, similar to Listing 23.3:

CODE: SELECT ALL
setListAdapter(new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1,
activities) {
public View getView(int pos, View convertView, ViewGroup parent) {
PackageManager pm = getActivity().getPackageManager();

            View v = super.getView(pos, convertView, parent);
            TextView tv = (TextView)v;
            ResolveInfo ri = getItem(pos);
            tv.setText(ri.loadLabel(pm));
            return v; 
        }
    });

End Quote

However, the comparing the code that I downloaded from the site to what was in the book, pm is set as final in the section that retrieves the list as follows:

	final PackageManager pm = getActivity().getPackageManager();
	List<ResolveInfo> activities = pm.queryIntentActivities(startupIntent, 0);

Will this also work and help to keep the app size down some?


#2

Yep!


#3

I’m having trouble trying to understand some of the code in Listing 23.4
Why do you create a method in the constructor of the ArrayAdapter? And why do we have to call super.getView instead of just getView?

I’m really enjoying the book.


#4

1- When you “create a method in the constructor” like that, you’re creating an anonymous inner class, just like you’ve been doing with View.OnClickListeners all along. The only difference here is that, instead of creating an implementation of an interface, you’re creating a subclass of ArrayAdapter.

2- When you call super.getView(), you’re saying, “I don’t want to call my implementation of getView(). I want to call my superclass’s implementation.”

This is a pretty important distinction in this case. If you call getView(), it will run your getView() implementation - which calls getView(), which runs your getView() implementation, which… you get the idea.


#5

(Oh - and thank you! We really appreciate it.)


#6

[quote=“phillips”]1- When you “create a method in the constructor” like that, you’re creating an anonymous inner class, just like you’ve been doing with View.OnClickListeners all along. The only difference here is that, instead of creating an implementation of an interface, you’re creating a subclass of ArrayAdapter.
[/quote]

Thanks for clarifying that. How come we didn’t qualify the declaration of getView() with ‘@Override’?

[code] ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, activities) {

        @Override
        public View getView(int pos, View convertView, ViewGroup parent) {[/code]

In other anonymous inner classes we’ve used @Override…for example, View.OnClickListener we use @Override when overriding onClick().

takePictureButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (mCamera != null) { mCamera.takePicture(mShutterCallback, null, mPictureCallback); } } });