My Toast Solution


#1

Just for the sake of it.

At this point I thought that as flickr returns more info than just a list of images that pertains to the group of images, I needed a way to encapsulate metadata, so I added this class

class FlickrResult {
    private long mItemsFound=0;
    private ArrayList<GalleryItem> mItems;

    public FlickrResult(){
        mItemsFound=0;
        mItems=new ArrayList<GalleryItem>();
    }

    void setItemsFound(long found) {
        mItemsFound=found;
    }


    public long getItemsFound() {
        return mItemsFound;
    }

    public ArrayList<GalleryItem> getItems() {
        return mItems;
    }

    public void add(GalleryItem item) {
        mItems.add(item);
    }
}

this encapsulates the GalleryItem list (which the Fragment feeds data into as it retrieves more rows, from a previous chapter challenge) along with the total (and in the future posibly more metadata.
Changing methods to return this object instead of the simple ArrayList lets me create and post the toast from the onPostExecute like so:

private class FetchItemsTask extends AsyncTask<Void, Void, ArrayList<GalleryItem>> {
         private String toastMessage=null;
        @Override
        protected ArrayList<GalleryItem> doInBackground(Void... params) {
            Activity lActivity = getActivity();

            if (lActivity == null)
                return new ArrayList<GalleryItem>();
            String query = PreferenceManager.getDefaultSharedPreferences(lActivity)
                    .getString(FlickrFetcher.PREF_SEARCH_QUERY, null);

            if (query != null) {
                FlickrResult lResult=new FlickrFetcher().search(query);
                toastMessage=lResult.getItemsFound()+" images found for \""+query+"\".";
                return lResult.getItems();
            } else {
                toastMessage = null;
                return new FlickrFetcher().fetchItems();
            }

        }

        @Override
        protected void onPostExecute(ArrayList<GalleryItem> galleryItems) {
            lastPageSize = galleryItems.size();
            if (mItems == null) {
                if(toastMessage != null) {
                              Toast.makeText(getActivity(),toastMessage, Toast.LENGTH_SHORT).show();
                }
                mItems = galleryItems;
                setUpAdapter();
            } else {
                mItems.addAll(galleryItems);
                ((ArrayAdapter) mGridView.getAdapter()).notifyDataSetChanged();
            }

        }
    }

The toastMessage code should be re-written to take into account localization later.

However since each page has the same api url (except for the page attribute) I wonder if the images don’t overlap if one query takes some time from the next page query.
That’s a problem about flickr’s api though.