Search doesn't work properly


#1

Hi, I have a problem with my search.

Whenever I open the app it shows the recent public photos, as it is supposed to show. However, when I open the search, type some query (like dog, London or USA) it accepts it, and shows the public photos again, NOT the query I asked for. Then I close app and open it again, it shows the result of my query. What may be the problem?
Any help will be appreciated!

Here are my snippets:

FlickrFetcher:

[code]public ArrayList downloadGalleryItems(String url) {

    ArrayList<GalleryItem> items = new ArrayList<GalleryItem>();
    try {
        String xmlString = getUrl(url);
        Log.i(TAG, "Received xml: " + xmlString);

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser parser = factory.newPullParser();
        parser.setInput(new StringReader(xmlString));

        parseItems(items, parser);

    } catch (IOException ioe) {
        Log.e(TAG, "Failed to fetch items", ioe);
    } catch (XmlPullParserException xppe) {
        Log.e(TAG, "Failed to parse", xppe);
    }

    return items;
}

public ArrayList<GalleryItem> fetchItems(String page) {
    String url = Uri.parse(ENDPOINT).buildUpon()
            .appendQueryParameter("method", METHOD_GET_RECENT)
            .appendQueryParameter("api_key", API_KEY)
            .appendQueryParameter(PARAM_EXTRAS, EXTRA_SMALL_URL)
            .appendQueryParameter(PAGE, page)
            .build().toString();
    return downloadGalleryItems(url);
}

public ArrayList<GalleryItem> search(String query) {
    String url = Uri.parse(ENDPOINT).buildUpon()
            .appendQueryParameter("method", METHOD_SEARCH)
            .appendQueryParameter("api_key", API_KEY)
            .appendQueryParameter(PARAM_EXTRAS, EXTRA_SMALL_URL)
            .appendQueryParameter(PARAM_TEXT, query)
            .build().toString();
    return downloadGalleryItems(url);
}


public void parseItems(ArrayList<GalleryItem> items, XmlPullParser parser) throws XmlPullParserException, IOException {

    int eventType = parser.next();

    while(eventType != XmlPullParser.END_DOCUMENT) {
        if(eventType == XmlPullParser.START_TAG && XML_PHOTO.equals(parser.getName())) {
                String id = parser.getAttributeValue(null, "id");
                String caption = parser.getAttributeValue(null, "title");
                String smallUrl = parser.getAttributeValue(null, EXTRA_SMALL_URL);

                GalleryItem item = new GalleryItem();
                item.setmId(id);
                item.setmUrl(smallUrl);
                item.setmCaption(caption);

                items.add(item);
        }

        eventType = parser.next();
    }
}[/code]

PhotoGalleryFragment:


private static final String TAG ="PhotoGalleryFragment";
    GridView mGridView;
    ArrayList<GalleryItem> mItems;
    int gridScrollPosition;
    int fetchedPage = 0;
    int currentPage = 1;
    ThumbnailDownloader<ImageView> mThumbnailDownloader;
    String query;

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.searchId:
                getActivity().onSearchRequested();
                return true;
            case R.id.cleanId:
                PreferenceManager.getDefaultSharedPreferences(getActivity())
                        .edit()
                        .putString(FlickrFetcher.PREF_SEARCH_QUERY, null)
                        .commit();
                updateItems();
                return true;
            default:
                return onOptionsItemSelected(item);
        }
    }

public void updateItems() {

        new FetchItemsTask().execute(String.valueOf(currentPage));
    }

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

           Activity activity = getActivity();
            if(activity == null) return new ArrayList<>();
                query = PreferenceManager.getDefaultSharedPreferences(activity)
                    .getString(FlickrFetcher.PREF_SEARCH_QUERY, null);
            if (query != null) {
                return new FlickrFetcher().search(query);
            } else {
                return new FlickrFetcher().fetchItems(params[0]);
            }
        }

MainActivity:

public class MainActivity extends ActionBarActivity {
    public static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PhotoGalleryFragment())
                    .commit();
        }

    }

    @Override
    protected void onNewIntent(Intent intent) {
        PhotoGalleryFragment fragment = (PhotoGalleryFragment) getSupportFragmentManager().findFragmentById(R.id.container);

        if(Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            Log.i(TAG, "Received a new search query: " + query);
            PreferenceManager.getDefaultSharedPreferences(this)
                    .edit()
                    .putString(FlickrFetcher.PREF_SEARCH_QUERY, query)
                    .commit();
        }

        fragment.updateItems();
    }
}

#2
@Override
        protected void onPostExecute(ArrayList<GalleryItem> items) {
            if (mItems == null) {
                mItems = items;
            } else {
                mItems.addAll(items);
            }

            mFetchedPage++;

            setupAdapter();
        }

After getting the recent public photos, you will have 100 photos in mItems. Then, you search something, the result will add to the mItems. So, the first thing you see is still the recent public photos and the search results are from the 101th photo.

when you close the app, the query you saved in SharedPreferences will not be delete. When you restart the app, it will load this query to initial the mItems.