0

AutoCompleteField在黑莓中使用过并使用BasicFilteredList. 它在黑莓 SDK 5.0 及更高版本中运行良好。但是,搜索是根据输入的单词进行的。如果我有如下文本字符串。

T 115 Centro Galleria 购物中心,Cnr Old Collier 和 Walters Road Morley WA 1522

它仅在我输入 152 时过滤字符串,但如果我使用 522 则无法过滤。我希望它应该根据我的要求进行过滤,因为它们存在于上述搜索字符串中。请帮我解决这个问题。下面是用于创建的代码示例AutoCompleteField

import net.rim.device.api.collection.util.BasicFilteredList;
import net.rim.device.api.collection.util.BasicFilteredListResult;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.AutoCompleteField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.text.TextFilter;
import net.rim.device.api.util.CharacterUtilities;

public class AutoCompleteFieldApp extends UiApplication {
    public static void main(String[] args) {
        AutoCompleteFieldApp app = new AutoCompleteFieldApp();
        app.enterEventDispatcher();
    }

    AutoCompleteFieldApp() {
        pushScreen(new HomeScreen());
    }
}

class HomeScreen extends MainScreen {
    LabelField selectedText;

    public HomeScreen() {
        setTitle("Autocomplete Text Field Demo");

         selectedText = new LabelField();

        BasicFilteredList filterList = new BasicFilteredList();
        String[] address = { "T 115 Centro Galleria Shopping Centre, Cnr Old Collier and Walters Road Morley WA 1522", 
                "1423 SEAVIEW POINT POINT COOK VIC 2674",
                "Lot 1498 Yarraman Road Wyndham Vale VIC 3795", 
                "Lot 3506 Witchmount Close Hillside VIC 4055",
                "6 Paas Place Williamstown VIC 4233", 
                "Lot 99 14 James Close Sunbury VIC 4502", 
                "1 Charlotte Street Clayton South VIC 4779" };

        filterList.addDataSet(1, address, "address", BasicFilteredList.COMPARISON_IGNORE_CASE);
        AutoCompleteField autoCompleteField = new AutoCompleteField(filterList){
            public void onSelect(Object selection, int SELECT_TRACKWHEEL_CLICK) {
                 ListField _list = getListField();
                 if (_list.getSelectedIndex() > -1) {
                     if(selectedText!=null){
                         BasicFilteredListResult result = (BasicFilteredListResult) selection;
                         selectedText.setText(result._object.toString());
                     }
                 }
             }
        };
        add(autoCompleteField);

        AutoCompleteField autoCompleteField2 = new AutoCompleteField(filterList){
            public void onSelect(Object selection, int SELECT_TRACKWHEEL_CLICK) {
                 ListField _list = getListField();
                 if (_list.getSelectedIndex() > -1) {
                     if(selectedText!=null){
                         BasicFilteredListResult result = (BasicFilteredListResult) selection;
                         selectedText.setText(result._object.toString()); 
                     }
                 }
             }
        };

        TextFilter filter = new TextFilter() {          
            public char convert(char c, int status) {
                if (!validate(c))
                    return 0;
                return c;
            }
            public boolean validate(char c) {
                return CharacterUtilities.isDigit(c);
            }
        };

        autoCompleteField2.getEditField().setFilter(filter);
        add(autoCompleteField2);
        add(selectedText);
    }
}

提前致谢。

4

1 回答 1

0

我没有尝试过,但我假设这应该可行。首先覆盖getMatchedKeywords你的BasicFilteredList. 您应该返回将触发匹配的关键字。所以对于你的情况"1522"你应该返回{"1522", "522", "22", "2"}。来自的对象BasicFilteredListResult应该是您的"1522"。正如您所看到的,如果您在可搜索的句子中有几个单词,那么生成所有这些子字符串会更加痛苦。但这是我认为的 API 的限制。

可能还有另一种解决方案。

于 2012-04-19T11:44:16.860 回答