Help Center

Local Navigation

Create an autocomplete text field from a data source

  1. Import the required classes and interfaces.
    import net.rim.device.api.ui.UiApplication;
    import net.rim.device.api.ui.container.MainScreen;
    import net.rim.device.api.ui.component.AutoCompleteField;
    import net.rim.device.api.collection.util.*;
  2. Create the application framework by extending the UiApplication class. In main(), create an instance of the new class and invoke enterEventDispatcher() to enable the application to receive events. In the application constructor, invoke pushScreen() to display the custom screen for the application. The HomeScreen class represents the custom screen that is described in step 3.
    public class AutoCompleteFieldApp extends UiApplication 
    {
        public static void main(String[] args) 
        {
            AutoCompleteFieldApp app = new AutoCompleteFieldApp();
            app.enterEventDispatcher();
        }
        public AutoCompleteFieldApp()
        {
        	   pushScreen(new HomeScreen());
        }
    }
  3. Create the custom screen for the application by extending the MainScreen class.
    class HomeScreen extends MainScreen
    {
        public HomeScreen()
        {
        }
    }
  4. In the screen constructor, create a BasicFilteredList object. Invoke addDataSource() to bind a data source to the BasicFilteredList. In this example, the data is contact information and the data source is the contact list. The first argument that you pass to addDataSource() is a unique ID. The second argument binds the BasicFilteredList object to a data source. The third argument specifies the set of data source fields to compare with. The fourth argument specifies the set of data source fields to make available when a match is found. In this example, the fields to compare with are the same as the fields to make available when a match is found. The fifth argument specifies the primary display field. The sixth argument specifies the secondary display field and is set to -1 if you do not want to use it. The final argument specifies a name for the BasicFilteredList; its value is generated automatically if you specify null.
    BasicFilteredList filterList = new BasicFilteredList();
            filterList.addDataSource(
            	1,
            	BasicFilteredList.DATA_SOURCE_CONTACTS,
    
                BasicFilteredList.DATA_FIELD_CONTACTS_NAME_FULL | 
                BasicFilteredList.DATA_FIELD_CONTACTS_COMPANY | 
                BasicFilteredList.DATA_FIELD_CONTACTS_EMAIL,
    
                BasicFilteredList.DATA_FIELD_CONTACTS_NAME_FULL | 
                BasicFilteredList.DATA_FIELD_CONTACTS_COMPANY | 
                BasicFilteredList.DATA_FIELD_CONTACTS_EMAIL,
    
                BasicFilteredList.DATA_FIELD_CONTACTS_NAME_FULL,
                -1,
                null);
  5. In the screen constructor, create an AutoCompleteField object. Pass the BasicFilteredList object that you created in step 4 to the AutoCompleteField constructor to bind the BasicFilteredList to the autocomplete text field. Invoke add() to add the field to the screen.
    AutoCompleteField autoCompleteField = new AutoCompleteField(filterList);
    add(autoCompleteField);

Code sample: Creating an autocomplete field from a data source

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.AutoCompleteField;
import net.rim.device.api.collection.util.*;


public class AutoCompleteFieldApp extends UiApplication 
{
    public static void main(String[] args) 
    {
         AutoCompleteFieldApp app = new AutoCompleteFieldApp();
         app.enterEventDispatcher();
    }
      
       AutoCompleteFieldApp()
       {
    	   HomeScreen scr = new HomeScreen();
    	   this.pushScreen(scr);
       }
}

class HomeScreen extends MainScreen
{
    public HomeScreen()
    {
        BasicFilteredList filterList = new BasicFilteredList();
        filterList.addDataSource(1,
        	BasicFilteredList.DATA_SOURCE_CONTACTS,
            BasicFilteredList.DATA_FIELD_CONTACTS_NAME_FULL | BasicFilteredList.DATA_FIELD_CONTACTS_COMPANY | BasicFilteredList.DATA_FIELD_CONTACTS_EMAIL,
            BasicFilteredList.DATA_FIELD_CONTACTS_NAME_FULL | BasicFilteredList.DATA_FIELD_CONTACTS_COMPANY | BasicFilteredList.DATA_FIELD_CONTACTS_EMAIL,
            BasicFilteredList.DATA_FIELD_CONTACTS_NAME_FULL,
            BasicFilteredList.DATA_FIELD_CONTACTS_NAME_FULL,
            null);
        
        AutoCompleteField autoCompleteField = new AutoCompleteField(filterList);
        add(autoCompleteField);
    }
}

Was this information helpful? Send us your comments.