Process search results

The SearchResponse object that you receive from the Unified Search Service structures your search results by the EntityBasedSearchable and search fields that matched your keywords. To access the data, you need to parse this object.
Before you begin: Ensure that UnifiedSearchServices.search() returned a SearchResponse object with data. Pass the SearchResponse to parseResponse().
  1. Import the required classes and interfaces.
    import net.rim.device.api.unifiedsearch.entity.*;
    import net.rim.device.api.util.Arrays;
    import net.rim.device.api.system.Application;
    import java.util.*;
    
  2. Create an instance variable to store searchable entities to display to the BlackBerry® device user.
    MySearchableEntity[] _myEntities;
  3. Implement parseResponse. This method runs after your search thread terminates, so declare the SearchResponse parameter as final.
    private void parseResponse(final SearchResponse searchResult) {
  4. Configure this method to run when your application regains control of the event thread.
        Application.getApplication().invokeLater(new Runnable() {
            public void run() {
  5. From the search results, retrieve a Hashtable object that contains search fields and searchable entities for your EntityBasedSearchable.
                final Hashtable results = searchResult.getSearchResult(_searchable);
  6. Retrieve an Enumeration object that contains the values in the Hashtable, and declare an array to store unique values from the enumeration.
                Enumeration values = results.elements();
                Object[] searchableEntities;
  7. Initialize your array of searchable entities to display.
                _myEntities = new MySearchableEntity[0];
  8. Iterate through the values enumeration and add unique values to searchableEntities.
                while(values.hasMoreElements()) {
                    searchableEntities = (Object[]) values.nextElement();
                    for(int i = searchableEntities - 1; i >= 0; --i) {
                        if(!Arrays.contains(_myEntities, searchableEntities[i]) {
                            Arrays.add(_myEntities, searchableEntities[i]);
                        }
                    }
                }
            }
        });
    } 
The _myEntities array contains a set of unique MySearchableEntity objects that you can use to display the search results to the user.

Was this information helpful? Send us your comments.