List of all Iphone and Blackberry Development codes in a one click Iphone,objective C,xcode,blackberry Development,iOS Development

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

4 comments:

  1. Thank you for the post Signare! How do you use the Field f = getFieldWithFocus() under Nav click to specify what each field will do. In android, I could use a String to get the position of the row clicked under OnItemClickListener, like String name = adapter1.getItem(position); and if (name.equals("Argentina")) { //do something }

    So with the above implementation, regardless of whether the search button is used, the right "MainScreen' is opened. (There was a bug in ListView in android, where after implementing search filter, and the result is brought to the first row, it opens the Activity for the original FirstRow item and not the one in the result under search) if you get my view.

    ReplyDelete
    Replies
    1. i am not aware of android. in this sample, it will display the clicked item's name.

      Delete
  2. Can't see how the Country class navigationClick method will ever be invoked - this is only used by Fields added to Screens and Country is a Data only class. Can you please clarify that?

    ReplyDelete