BlackBerry 6:ListFieldCallback.indexOfList() - 如何在键入时进行过滤?

发布于 2024-10-29 20:35:01 字数 3328 浏览 6 评论 0原文

我正在尝试在其下方显示一个 TextField 和一个 ListField:

在此处输入图像描述

我想过滤(又名“实时搜索”)当用户在文本字段中输入单词时显示的行数。

我尝试致电 ListField.setSearchable(true) 但它不会改变任何内容,即使我在 ListField 聚焦时输入单词。

顺便说一句,我想知道要采用哪个 TextField。我使用了 AutoCompleteField,因为它看起来与我想要的字段完全一样(带圆角的白色字段),但它可能不是最佳选择(因为我在键入时不需要 AutoCompleteField 的下拉列表)。

这是我当前的代码 -

MyScreen.java:

private ListField presetListField = new ListField();
private MyList presetList = new MyList(presetListField);

private MyScreen() {
    int size;

    getMainManager().setBackground(_bgOff);
    setTitle("Favorites");

    BasicFilteredList filterList = new BasicFilteredList();        
    String[] days = {"Monday", "Tuesday", "Wednesday",
                     "Thursday", "Friday", "Saturday", "Sunday"};
    int uniqueID = 0;
    filterList.addDataSet(uniqueID, days, "days", 
        BasicFilteredList.COMPARISON_IGNORE_CASE);

    // XXX probably a bad choice here?
    AutoCompleteField autoCompleteField = 
        new AutoCompleteField(filterList);
    add(autoCompleteField);        

    presetListField.setEmptyString("* No Favorites *", DrawStyle.HCENTER);
    add(presetListField);

    presetList.insert("Monday");
    presetList.insert("Tuesday");
    presetList.insert("Wednesday");
    for (int i = 0; i < 16; i++) {
        presetList.insert("Favorite #" + (1 + i));
    }
}

MyList.java:

public class MyList implements ListFieldCallback {
    private Vector _preset = new Vector();
    private ListField _list;

    public MyList(ListField list) {
        _list = list;
        _list.setCallback(this);
        _list.setRowHeight(-2);
        // XXX does not seem to have any effect
        _list.setSearchable(true);
    }

    public void insert(String str) {
        insert(str, _preset.size());
    }

    public void insert(String str, int index) {
        _preset.insertElementAt(str, index);
        _list.insert(index);
    }

    public void delete(int index) {
        _preset.removeElementAt(index);
        _list.delete(index);
    }

    public void drawListRow(ListField listField, 
      Graphics g, int index, int y, int width) {
        Font f = g.getFont();
        Font b = f.derive(Font.BOLD, f.getHeight() * 2);
        Font i = f.derive(Font.ITALIC, f.getHeight());

        g.setColor(Color.WHITE);
        g.drawText((String)_preset.elementAt(index), Display.getWidth()/3, y);

        g.setFont(i);
        g.setColor(Color.GRAY);
        g.drawText("Click to get frequency", 
           Display.getWidth()/3, y + g.getFont().getHeight());

        g.setFont(b);
        g.setColor(Color.YELLOW);
        g.drawText(String.valueOf(100f + index/10f), 0, y);
     }

     public Object get(ListField list, int index) {
         return _preset.elementAt(index);
     }

     public int indexOfList(ListField list, String prefix, int start) {
         return _preset.indexOf(prefix, start);
     }

     public int getPreferredWidth(ListField list) {
         return Display.getWidth();
     }
}

谢谢! 亚历克斯

I'm trying to display a TextField and a ListField below it:

enter image description here

And I would like to filter (aka "live search") the number of displayed rows, while the user is typing a word into the TextField.

I've tried calling ListField.setSearchable(true) but it doesn't change anything, even if I type words while having the ListField focussed.

And by the way I wonder which TextField to take. I've used AutoCompleteField because it looks exactly as I want the field to be (white field with rounded corners), but it is probably not the best choice (because I don't need AutoCompleteField's drop down list while typing).

Here is my current code -

MyScreen.java:

private ListField presetListField = new ListField();
private MyList presetList = new MyList(presetListField);

private MyScreen() {
    int size;

    getMainManager().setBackground(_bgOff);
    setTitle("Favorites");

    BasicFilteredList filterList = new BasicFilteredList();        
    String[] days = {"Monday", "Tuesday", "Wednesday",
                     "Thursday", "Friday", "Saturday", "Sunday"};
    int uniqueID = 0;
    filterList.addDataSet(uniqueID, days, "days", 
        BasicFilteredList.COMPARISON_IGNORE_CASE);

    // XXX probably a bad choice here?
    AutoCompleteField autoCompleteField = 
        new AutoCompleteField(filterList);
    add(autoCompleteField);        

    presetListField.setEmptyString("* No Favorites *", DrawStyle.HCENTER);
    add(presetListField);

    presetList.insert("Monday");
    presetList.insert("Tuesday");
    presetList.insert("Wednesday");
    for (int i = 0; i < 16; i++) {
        presetList.insert("Favorite #" + (1 + i));
    }
}

MyList.java:

public class MyList implements ListFieldCallback {
    private Vector _preset = new Vector();
    private ListField _list;

    public MyList(ListField list) {
        _list = list;
        _list.setCallback(this);
        _list.setRowHeight(-2);
        // XXX does not seem to have any effect
        _list.setSearchable(true);
    }

    public void insert(String str) {
        insert(str, _preset.size());
    }

    public void insert(String str, int index) {
        _preset.insertElementAt(str, index);
        _list.insert(index);
    }

    public void delete(int index) {
        _preset.removeElementAt(index);
        _list.delete(index);
    }

    public void drawListRow(ListField listField, 
      Graphics g, int index, int y, int width) {
        Font f = g.getFont();
        Font b = f.derive(Font.BOLD, f.getHeight() * 2);
        Font i = f.derive(Font.ITALIC, f.getHeight());

        g.setColor(Color.WHITE);
        g.drawText((String)_preset.elementAt(index), Display.getWidth()/3, y);

        g.setFont(i);
        g.setColor(Color.GRAY);
        g.drawText("Click to get frequency", 
           Display.getWidth()/3, y + g.getFont().getHeight());

        g.setFont(b);
        g.setColor(Color.YELLOW);
        g.drawText(String.valueOf(100f + index/10f), 0, y);
     }

     public Object get(ListField list, int index) {
         return _preset.elementAt(index);
     }

     public int indexOfList(ListField list, String prefix, int start) {
         return _preset.indexOf(prefix, start);
     }

     public int getPreferredWidth(ListField list) {
         return Display.getWidth();
     }
}

Thank you!
Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文