Searching an Array List

Searching an array list:

// When we click the search button.
public void searchACity (View searchView) {

    // Set our boolean to false.
    searchYes = false;

    // Defining an array of titles and urls.
    searchCityList = new ArrayList<String>();
    searchCityUrl = new ArrayList<String>();

    // Clear the lists, so we don't get duplicates.
    searchCityList.clear();
    searchCityUrl.clear();

    // Get our text and turn it into a string that is lowercase.
    String search = searchMyLocal.getText().toString().toLowerCase();

    // Testing only // Log.i("WJH", search);

    // Okay, now search the citylist for that string.
    for (int i = 0; i < cityList.size(); i++) {

        // If it contains the search string....
        if (cityList.get(i).contains(search)) {

            // Testing only // Log.i("WJH", cityList.get(i) + urlList.get(i));

            // Then add it to our new search lists.
            searchCityList.add(cityList.get(i));
            searchCityUrl.add(urlList.get(i));

            // And set our search boolean to true, because we found something.
            searchYes = true;

        }
    }

    // So, if we found something....
    if (searchYes) {

        // Clear the old lists.
        cityList.clear();
        urlList.clear();

        // For each item in our new search lists....
        for (int g = 0; g < searchCityList.size(); g++) {

            // Add them to our lists.
            cityList.add(searchCityList.get(g));
            urlList.add(searchCityUrl.get(g));

            // And notify of an update and update the list view.
            addaptedAray.notifyDataSetChanged();
            theList.invalidateViews();

        }

    } else { // But if nothing found....

        // The only way to see this, is if the search yielded no results.
        Toast.makeText(getApplicationContext(), "Nothing found that matches your search.", Toast.LENGTH_SHORT).show();

        // Reset the list
        addCities();

        // Update the list view.
        addaptedAray.notifyDataSetChanged();
        theList.invalidateViews();

    }

    // Get rid of the keyboard, so they can see the results...
    InputMethodManager imm = (InputMethodManager)getSystemService(getApplicationContext().INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);

} // searchACity end.

In my Just Craigslist app, the user used to select their location from a list. I thought that was a good system when I made the app, because there is only one Craigslist city choice in my area. Once I chose my location, the preferences were saved in the app, so I never needed to change it.citysearch

However, it was pointed out to me that some people live close to two or more Craigslist areas, and have to search between these areas, constantly choosing another location. With the long list of places to choose from, this became very tedious.

So, I needed a way to search the list. Not only that, but I needed a way to re-list the searched passing criteria, and that is what I have done in the above code. Hopefully people will find that intuitive.

Essentially, I am taking the array lists that existed, and searching them with this line:

// If it contains the search string....
        if (cityList.get(i).contains(search)) {

Which I just looped through every city in the list, seeing if it contains the search criteria. If it does, then I add it to a temporary list. After the loop has finished searching, Android then clears the master list, and adds the temporary list to it, and refreshes the view, so now you can choose from your list of matching criteria. Ex: if you search for Georgia, you will be given a list of all the cities in Georgia that have a Craigslist area.

There were two problems:

  1. Case sensitive. The search is case sensitive. I solved this by forcing all queries to be lower case, and edited the city list to be all lower case as well. Even if they type in all caps, it will only search in lower case.
  2. No results. Some searches will naturally yield no results. Obviously, if you search for places on Mars or Saturn, you wont find them here on Earth, and certainly not in the Craigslist listings. So, when the search fails, you are given the original entire list again, as well as a popup to say that nothing was found.

It is my hope that this will make it easier for people to change locations. Only time will tell.

Linux – keep it simple.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s