【问题标题】:BlackBerry - How to attach and get particular Field id in KeywordFilterFieldBlackBerry - 如何在 KeywordFilterField 中附加和获取特定字段 id
【发布时间】:2014-03-21 07:53:15
【问题描述】:

我已经设置了一个 KeywordFilterField(感谢 Signare - http://rincethomas.blogspot.in/2012/04/search-field-in-bb.html)并且一切正常,但是,我想知道如何让他们做某事或在按下/点击时打开一个新屏幕。有人(也是Signare)建议我将字段ID附加到焦点字段,然后在单击时使用该值打开新屏幕,但我不知道如何实现这一点。

下面是我的代码:

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

}

请帮忙。

【问题讨论】:

    标签: java blackberry blackberry-simulator blackberry-eclipse-plugin blackberry-jde


    【解决方案1】:

    这将显示点击的国家名称。

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

    【讨论】:

    • 你能更新你的博客文章以更正那里的代码吗?
    猜你喜欢
    • 2023-03-31
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多