Help Center

Local Navigation

Create a text list that can be filtered

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. A word that a BlackBerry® device user enters into the text input field filters the elements in the list. For more information about using the KeywordFilterField class, see the KeywordFilterFieldDemo sample, included with the BlackBerry® Java® Development Environment version 4.3.1.
  1. Import the following classes:
    • net.rim.device.api.ui.component.KeywordFilterField
    • net.rim.device.api.collection.util.SortedReadableList
    • net.rim.device.api.collection.util.SortedReadableList
    • java.util.Vector
    • java.io.InputStream
    • net.rim.device.api.io.LineReader
    • java.lang.String
  2. Import the net.rim.device.api.ui.component.KeywordProvider interface.
  3. 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;
  4. To create a list of selectable text items, populate a vector with data from a text file.
    _countries = getDataFromFile();
  5. Create an instance of a class that extends the SortedReadableList class.
    _CountryList = new CountryList(StringComparator.getInstance(true),_countries);
  6. To specify the elements of the list, create a new instance of a KeywordFilterField object.
    _keywordField = new KeywordFilterField();
  7. Invoke KeywordFilterField.setList().
    _keywordField.setList(_CountryList, _CountryList);
  8. Set a label for the input field of the KeywordFilterFIeld.
    _keywordField.setLabel("Search: ");
  9. Create the main screen of the application and add a KeywordFilterField to the main screen.
    KeywordFilterDemoScreen screen = new KeywordFilterDemoScreen(this,_keywordField);
    screen.add(_keywordField.getKeywordField());
    screen.add(_keywordField);
    pushScreen(screen);
    
  10. 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()
        {
  11. Create and store a reference to a new Vector object.
    Vector countries = new Vector();
  12. Create an input stream to the text file.
    InputStream stream = getClass().getResourceAsStream("/Data/CountryData.txt");
  13. Read CRLF delimited lines from the input stream.
    LineReader lineReader = new LineReader(stream);             
  14. 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.
    for(;;)
    {   //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
      {
       break;
       }
     }  // end the for loop
     return countries;    
    }
  15. To add a keyword to the list of selectable text items, invoke SortedReadableList.doAdd(element).
    SortedReadableList.doAdd(((Country)countries.elementAt(i)).getCountryName()) ;
  16. To update the list of selectable text items, invoke KeywordFilterField.updateList().
    _keywordField.updateList();
  17. To obtain the key word that a BlackBerry device user typed into the KeywordFilterField, invoke KeywordFilterField.getKeyword().
    String userTypedWord = _keywordField.getKeyword();

Index


Was this information helpful? Send us your comments.