2011-04-05 49 views
0

我想顯示一個文本字段和它下面一個ListField:BlackBerry 6:ListFieldCallback.indexOfList() - 如何在輸入時過濾?

enter image description here

而且我想過濾(又名「生活搜索」)中顯示的行數,當用戶鍵入單詞放入TextField中。

我試過撥打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(); 
    } 
} 

謝謝! Alex

回答