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().
-
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.*;
-
Create an instance variable to store searchable entities to
display to the
BlackBerry®
device user.
MySearchableEntity[] _myEntities;
-
Implement
parseResponse. This method runs after your search
thread terminates, so declare the
SearchResponse parameter as
final.
private void parseResponse(final SearchResponse searchResult) {
-
Configure this method to run when your application regains control
of the event thread.
Application.getApplication().invokeLater(new Runnable() {
public void run() {
-
From the search results, retrieve a
Hashtable object that contains search fields and
searchable entities for your
EntityBasedSearchable.
final Hashtable results = searchResult.getSearchResult(_searchable);
-
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;
-
Initialize your array of searchable entities to display.
_myEntities = new MySearchableEntity[0];
-
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.