Note that there are some explanatory texts on larger screens.

plurals
  1. POBlackBerry 6: ListFieldCallback.indexOfList() - how to filter while typing?
    text
    copied!<p>I'm trying to display a TextField and a ListField below it:</p> <p><img src="https://i.stack.imgur.com/5DVIO.png" alt="enter image description here"></p> <p>And I would like to filter (aka "live search") the number of displayed rows, while the user is typing a word into the TextField.</p> <p>I've tried calling <a href="http://www.blackberry.com/developers/docs/4.6.0api/net/rim/device/api/ui/component/ListField.html#setSearchable%28boolean%29" rel="nofollow noreferrer">ListField.setSearchable(true)</a> but it doesn't change anything, even if I type words while having the ListField focussed.</p> <p>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).</p> <p>Here is my current code -</p> <p><strong>MyScreen.java:</strong></p> <pre><code>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 &lt; 16; i++) { presetList.insert("Favorite #" + (1 + i)); } } </code></pre> <p><strong>MyList.java:</strong></p> <pre><code>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(); } } </code></pre> <p>Thank you! Alex</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload