javablackberryblackberry-simulatorblackberry-eclipse-pluginblackberry-jde

BlackBerry - How to attach and get particular Field id in KeywordFilterField


I have set up a KeywordFilterField (Thanks to Signare - http://rincethomas.blogspot.in/2012/04/search-field-in-bb.html) and everything works, however, I would like to know how to make them do something or open a new screen when pressed / clicked. Someone (also Signare) suggested I attach a field id to the field in focus and then use that value to open new screen when clicked, but I do not know how to implement this.

Below is my code:

public class IndexScreen extends MainScreen {

    KeywordFilterField _keywordFilterField;
    CountryList _countryList;

    public IndexScreen() {
        _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();
        _keywordFilterField.setLabel("");
        _keywordFilterField.setSourceList(_countryList, _countryList);

        setTitle(_keywordFilterField.getKeywordField());
        add(_keywordFilterField);
        // this.addMenuItem(addElementItem);

        // add(this);
    }

    void addElementToList(Country country) {
        _countryList.addElement(country);
        _keywordFilterField.updateList();
    }

    private final MenuItem addElementItem = new MenuItem("Add country", 0, 0) {
        public void run() {
            _keywordFilterField.setKeyword("");

            String[] selections = { "Add", "Cancel" };
            Dialog addDialog = new Dialog("Add Country", selections, null, 0,
                    null);
            EditField inputField = new EditField("Country: ", "");
            addDialog.add(inputField);

            if (addDialog.doModal() == 0) {
                addElementToList(new Country(inputField.getText()));
            }
        }
    };
}

class SearchFieldDemoScreen extends MainScreen {
    public SearchFieldDemoScreen() {
    };
}

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

    protected boolean navigationClick(int status, int time) {

        Field f = getFieldWithFocus();
        {
            return true;
        }
    }

    private Field getFieldWithFocus() {
        // TODO Auto-generated method stub
        return null;
    }

}

Please help.


Solution

  • This will display the clicked country name.

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