List of all Iphone and Blackberry Development codes in a one click Iphone,objective C,xcode,blackberry Development,iOS Development
Showing posts with label Search field. Show all posts
Showing posts with label Search field. Show all posts

Monday, April 16, 2012

Search field in Blackberry

When user click on a specific Alphabet key, then it will show the country name that have the starting letter corresponding to the Key pressed.



package mypackage;

import net.rim.device.api.collection.util.SortedReadableList;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.KeywordFilterField;
import net.rim.device.api.ui.component.KeywordProvider;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.util.Comparator;
import net.rim.device.api.util.StringUtilities;

public final class MyScreen extends MainScreen
{
    KeywordFilterField _keywordFilterField;
    CountryList _countryList;
public MyScreen(){
 _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(){
             protected boolean navigationClick(int status, int time) {
                 int index = getSelectedIndex();
               
                 String str = (((Country) getElementAt(index)).toString());
                 Dialog.alert(str);
               
                return true;
             }
        };
        _keywordFilterField.setLabel("");
        _keywordFilterField.setSourceList(_countryList, _countryList);
          add(_keywordFilterField);
      }
 
}

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;
    }
  

}