Help Center

Local Navigation

Create a search field

You can create an application that uses the KeywordFilterField class, included in the net.rim.device.api.ui.component package, to provide a UI field that consists of a single text input field and a list of selectable elements. As users type text in a search field, the application filters the elements in the list that begin with the search text. For more information about using the KeywordFilterField class, see the Keyword Filter Field sample application, included with the BlackBerry® Java® Development Environment version 4.3.1 or later.
  1. Import the required classes and interfaces.
    import net.rim.device.api.collection.util.SortedReadableList;
    import net.rim.device.api.ui.component.KeywordFilterField;
    import net.rim.device.api.ui.component.KeywordProvider;
    import java.lang.String;
    import java.util.Vector;
  2. Create variables. In the following code sample, CountryList extends the SortedReadableList class and implements the KeywordProvider interface.
    private KeywordFilterField _keywordField;    
    private CountryList _CountryList;  
    private Vector _countries;
  3. To create a list of selectable text items, populate a vector with data from a text file.
    _countries = getDataFromFile();
  4. Create an instance of a class that extends the SortedReadableList class.
    _CountryList = new CountryList(StringComparator.getInstance(true),_countries);
  5. To specify the elements of the list, create a new instance of a KeywordFilterField object.
    _keywordField = new KeywordFilterField();
  6. Invoke KeywordFilterField.setList().
    _keywordField.setList(_CountryList, _CountryList);
  7. Set a label for the input field of the KeywordFilterFIeld.
    _keywordField.setLabel("Search: ");
  8. Create the main screen of the application and add a KeywordFilterField to the main screen.
    KeywordFilterDemoScreen screen = new KeywordFilterDemoScreen(this,_keywordField);
  9. To create a method that populates and returns a vector of Country objects containing data from text file, in the method signature, specify Vector as the return type.
    public Vector getDataFromFile()
  10. Create and store a reference to a new Vector object.
    Vector countries = new Vector();
  11. Create an input stream to the text file.
    InputStream stream = getClass().getResourceAsStream("/Data/CountryData.txt");
  12. Read CRLF delimited lines from the input stream.
    LineReader lineReader = new LineReader(stream);             
  13. Read data from the input stream one line at a time until you reach the end of file flag. Each line is parsed to extract data that is used to construct Country objects.
        //Obtain a line of text from the text file
        String line = new String(lineReader.readLine());
        //If we are not at the end of the file, parse the line of text
        if(!line.equals("EOF")) {
            int space1 = line.indexOf(" ");
            String country = line.substring(0,space1);                        
            int space2 = line.indexOf(" ",space1+1);
            String population = line.substring(space1+1,space2);                       
            String capital = line.substring(space2+1,line.length());
            // Create a new Country object
            countries.addElement(new Country(country,population,capital));                                        
        else {
    } // end the for loop
    return countries;
  14. To add a keyword to the list of selectable text items, invoke SortedReadableList.doAdd(element).
    SortedReadableList.doAdd(((Country)countries.elementAt(i)).getCountryName()) ;
  15. To update the list of selectable text items, invoke KeywordFilterField.updateList().
  16. To obtain the key word that a BlackBerry device user typed into the KeywordFilterField, invoke KeywordFilterField.getKeyword().
    String userTypedWord = _keywordField.getKeyword();

Was this information helpful? Send us your comments.