Help Center

Local Navigation

Create an autocomplete text field from a data set

  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 constructor, invoke pushScreen() to display the custom screen for the application. The HomeScreen class, described in step 3, represents the custom screen.
    public class AutoCompleteFieldApp extends UiApplication 
    {
        public static void main(String[] args) 
        {
            AutoCompleteFieldApp app = new AutoCompleteFieldApp();
            app.enterEventDispatcher();
        }
          
        AutoCompleteFieldApp()
        {
        	   pushScreen(new HomeScreen());
        }
    }
  3. Create the custom screen by extending the MainScreen class.
    class HomeScreen extends MainScreen
    {
        public HomeScreen()
        {
        }
    }
  4. In the constructor, create a BasicFilteredList object. Create a String array and store the strings that you want to match against in the array. In this example, the strings are days of the week. Invoke addDataSet() to bind the data in the array to the BasicFilteredList.
    BasicFilteredList filterList = new BasicFilteredList();
    String[] days = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
    filterList.addDataSet(1,days,"days",BasicFilteredList.COMPARISON_IGNORE_CASE);
  5. In the constructor, create an AutoCompleteField object. Pass an instance of the BasicFilteredList 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 set

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();
        String[] days = {"Monday","Tuesday","Wednesday",
        		         "Thursday","Friday","Saturday","Sunday"};
        filterList.addDataSet(1,days,"days",BasicFilteredList.COMPARISON_IGNORE_CASE);
        AutoCompleteField autoCompleteField = new AutoCompleteField(filterList);
        add(autoCompleteField);
    }
}
Previous topic: Autocomplete text field

Was this information helpful? Send us your comments.