Toast Solution


#1

I have to agree with Rick this chapter needs help - as someone who has written CompSci books I know how hard it is especially near the end to make each chapter as perfect as the ones the author championed earlier in the writing process :wink: I find the behavior of the sample code very strange, especially when I hit the search button, enter “dogs”, and then hit the virtual keyboard “search button” to tell it to perform the search. First the background becomes Brian’s image, then they are replaced with the items of the original preceding search which was already there, then when the search data comes in the screen is again replaced with the photos matching the search. A very unsettling experience for the user (on a Galaxy S4). Maybe much of this is due to network lag and the fact that my LRU cache from the previous chapter challenge is not integrated but it sure seems weird to see the Brian matrix, then a couple tils of the last search, then results of the new search coming in.

Also the search text on the action bar stays in place after the search - I think after the search the UI should return to “non-search” mode and display the search and X icons again. No idea how to do this - a suggestion would be appreciated.

As for the toast challenge the difficulty was not in grabbing the “total” field from the XML returned from Flickr, but in getting it to the main thread for the toast. I used the PreferencesManager and Shared Preferences to store it in the AsyncTask, but this also required me to pass the Activity Context to the AsyncTask routines fetchItems() and search() since I could not figure out a way to get access to the main thread context to use the PreferencesManager!

In the onPostExecute() method back on the main thread I grabbed the total stored by the AsyncTask, easily toasted it, and also cleared the PM key back to null.

What other manner of IPC might have been better for sending the total back to requesting task without redoing the interface? Obviously instead of just returning the ArrayAdapter it would be best to return an encapsulating object that contains both the ArrayAdapter AND any other ancillary info about the array contents as the Flickr API does return in the top level record requested. But are there other IPC that would be easy as what I did with the PM Shared Preferences?


#2

Also one other question - in this chap and chap 28 what should happen on an S4 if the lower left “menu” button is pressed? I am not seeing an options menu nor a “clear” menu option. It is all on the action bar with or without the SearchView enabled.

If I long press the menu button I get a search dialog with back arrow that does properly collapse the search field on action bar, but not when I activate search via action bar. Why diff behaviors?


#3

Well I have an answer for my 2nd post - the long press is bringing up non-SearchView dialog.

Also I am finding after I search with the SearchView, when I hit “X” to clear it the action bar goes to the “Search Hint”. Another touch of the ‘X’ closes it the way I want. How can I make the SearchView close back to icon-only when the keyboard search button is struck?


#4

I just stored the total results as a public class variable in FlickrFetchr, but I wonder if there’s a better way of doing it. Doing that makes it easy to access though, as I have an instance of FlickrFetchr in PhotoGalleryFragment called mfetcher so I just use the value of mfetcher.totalResults in my Toast. However, doing that probably breaks some program design conventions…


#5

yes i do that too

heres my onPostExecute

@Override protected void onPostExecute(ArrayList<GalleryItem> items) { mItems = items; setupAdapter(); Toast.makeText(getActivity(), FlickrFetchr.TOTALO, Toast.LENGTH_SHORT).show(); }

if someone have better code please feel free to share it with me :slight_smile:


#6

in FlickrFetchr.java

    private String mTotalSearch;

    public String getTotalSearch() {
        return mTotalSearch;
    }

in PhotoGalleryFragment.java


    private String mTotalSearch;

    private class FetchItemsTask extends AsyncTask<Integer, Void, ArrayList<GalleryItem>> {

        @Override
        protected ArrayList<GalleryItem> doInBackground(Integer... params) {

            Activity activity = getActivity();

            if (activity == null) {
                return new ArrayList<>();
            }

            String query = PreferenceManager.getDefaultSharedPreferences(activity)
                    .getString(FlickrFetchr.PREF_SEARCH_QUERY, null);

            if (query != null) {

                FlickrFetchr flickrFetchr = new FlickrFetchr();
                ArrayList<GalleryItem> items = flickrFetchr.search(query, params[0]);
                mTotalSearch = flickrFetchr.getTotalSearch();
                return items;
            } else {
                mTotalSearch = null;
                return new FlickrFetchr().fetchItems(params[0]);
            }
        }

        @Override
        protected void onPostExecute(ArrayList<GalleryItem> items) {

            if (mTotalSearch != null) {
                Toast.makeText(getActivity(), mTotalSearch,
                        Toast.LENGTH_LONG).show();
            }

            if (mItems == null) {
                mItems = items;
            } else {
                mItems.addAll(items);
            }

            mFetchedPage++;

            setupAdapter();
        }
    }