Search field

Use a search field to allow BlackBerry® device users to filter a list of words that match the characters that they type in the field. Users can select one of the presented matches or continue typing to further restrict the available choices.

When you create a search field, you must specify a source list. The source list supplies the strings to compare against, which can be hard-coded or derived from data sources on a BlackBerry device, such as contacts, memos, and tasks.

Class KeywordFilterField
Supported since BlackBerry® Java® SDK 4.5
Example This screen shows a search field.

For more information about search fields, see the UI Guidelines.

Code sample: Creating a search field

import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.collection.ReadableList;
import net.rim.device.api.util.Comparator;
import java.util.*;
import net.rim.device.api.collection.util.*; 
import net.rim.device.api.util.StringUtilities;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;

public class SearchFieldDemo extends UiApplication
{
    KeywordFilterField _keywordFilterField;    
    CountryList _countryList;

    public static void main(String args[])
    {
        SearchFieldDemo theApp = new SearchFieldDemo();
        theApp.enterEventDispatcher();
    }
    
    public SearchFieldDemo()
    {
        _countryList = new CountryList();
        _countryList.addElement(new Country("Zimbabwe"));
        _countryList.addElement(new Country("Argentina"));
        _countryList.addElement(new Country("Brazil"));
        _countryList.addElement(new Country("Canada"));
        _countryList.addElement(new Country("Chile"));
        _countryList.addElement(new Country("China"));
        _countryList.addElement(new Country("Germany"));
        
        _keywordFilterField = new KeywordFilterField();
        _keywordFilterField.setLabel("");
        _keywordFilterField.setSourceList(_countryList, _countryList);
        
        SearchFieldDemoScreen screen = new SearchFieldDemoScreen();
        screen.setTitle(_keywordFilterField.getKeywordField());
        screen.add(_keywordFilterField);
        screen.addMenuItem(addElementItem);
        
        pushScreen(screen);    
    }
    
    void addElementToList(Country country)
    {       
         _countryList.addElement(country);
        _keywordFilterField.updateList();       
    }   
    
    private final MenuItem addElementItem = new MenuItem("Add country", 0, 0)
    {  
        public void run()
        { 
            _keywordFilterField.setKeyword("");             

            String[] selections = {"Add","Cancel"};
            Dialog addDialog = new Dialog("Add Country", selections, null, 0, null);
            EditField inputField = new EditField("Country: ","");
            addDialog.add(inputField);
            
            if(addDialog.doModal() == 0)
            {
                addElementToList(new Country(inputField.getText()));                                                     
            }
        }
    };
}

class SearchFieldDemoScreen extends MainScreen
{
    public SearchFieldDemoScreen(){};
} 

class CountryList extends SortedReadableList implements KeywordProvider
{
    public CountryList()
    {
        super(new CountryListComparator());        
    } 
   
    void addElement(Object element)
    {
        doAdd(element);        
    }
    
    public String[] getKeywords(Object element)
    {
        if(element instanceof Country)
        {
            return StringUtilities.stringToWords(element.toString());
        }
        return null;
    }

    final static class CountryListComparator implements Comparator
    {
        public int compare(Object o1, Object o2)
        {
            if (o1 == null || o2 == null)
                throw new IllegalArgumentException("Cannot compare null countries");
        
            return o1.toString().compareTo(o2.toString());
        }
    }
    
}
 
class Country
{
    private String _countryName;
    
    public Country(String countryName)
    {
        _countryName = countryName;        
    }
    
    public String toString()
    {
        return _countryName;
    }
}
Previous topic: Email address field

Was this information helpful? Send us your comments.