Help Center
Local Navigation
- Creating a UI that is consistent with standard BlackBerry UIs
- BlackBerry device user input and navigation
- Screens
- Accelerometer
- Events
- Command Framework API
- Arranging UI components
-
UI components
- Add a UI component to a screen
- Aligning a field to a line of text
- Buttons
- Check boxes
- Create a bitmap
- Create a custom field
- Creating a field to display web content
- Dialog boxes
- Drop-down lists
- Labels
- Lists and tables
- Radio buttons
- Activity indicators and progress indicators
- Pickers
- Search
- Spin boxes
- Text fields
- Tree views
- Images
- Menu items
- Custom fonts
- Spelling checker
- Related resources
- Glossary
- Provide feedback
- Document revision history
- Legal notice
BlackBerry Manuals & Help
>
Developer Documentation
>
Java Development Guides and API Reference
>
Development Guide
UI and Navigation - BlackBerry Java SDK - 6.0
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.
-
Import the required classes and interfaces.
import net.rim.device.api.collection.util.SortedReadableList; import net.rim.device.api.io.LineReader; import net.rim.device.api.ui.component.KeywordFilterField; import net.rim.device.api.ui.component.KeywordProvider; import java.io.InputStream; import java.lang.String; import java.util.Vector;
-
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;
-
To create a list of selectable text items, populate a vector with
data from a text file.
_countries = getDataFromFile();
-
Create an instance of a class that extends the
SortedReadableList class.
_CountryList = new CountryList(StringComparator.getInstance(true),_countries);
-
To specify the elements of the list, create a new instance of a
KeywordFilterField object.
_keywordField = new KeywordFilterField();
-
Invoke
KeywordFilterField.setList().
_keywordField.setList(_CountryList, _CountryList);
-
Set a label for the input field of the
KeywordFilterFIeld.
_keywordField.setLabel("Search: "); -
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);
-
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()
-
Create and store a reference to a new
Vector object.
Vector countries = new Vector();
-
Create an input stream to the text file.
InputStream stream = getClass().getResourceAsStream("/Data/CountryData.txt"); -
Read CRLF delimited lines from the input stream.
LineReader lineReader = new LineReader(stream);
-
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; -
To add a keyword to the list of selectable text items, invoke
SortedReadableList.doAdd(element).
SortedReadableList.doAdd(((Country)countries.elementAt(i)).getCountryName()) ;
-
To update the list of selectable text items, invoke
KeywordFilterField.updateList().
_keywordField.updateList();
-
To obtain the key word that a
BlackBerry device user typed into the
KeywordFilterField, invoke
KeywordFilterField.getKeyword().
String userTypedWord = _keywordField.getKeyword();
Next topic: Autocomplete text field
Previous topic: Best practice: Implementing search