You can use an autocomplete field which will work on device OS 5.0 onwards. If you want your app to work on devices 4.5Os onwards , let me know and i will update the code
Vector box1 = new Vector();
Enumeration iterator = vector.elements();
int i = 0;
final Object[] objs = new Object[v.size()];
while (iterator.hasMoreElements()) {
objs[i] = (String) iterator.nextElement();
i++;
}
BasicFilteredList filterList = new BasicFilteredList();
filterList.setMinimumRefreshInterval(250);
filterList.addDataSet(1, objs, "names",
BasicFilteredList.COMPARISON_IGNORE_CASE);
AutoCompleteField autoCompleteField = new AutoCompleteField(
filterList, AutoCompleteField.LIST_STATIC);
add(autoCompleteField);
This code will list all the strings in the vector and as you type,filter the results.
If you want to draw Check box you can over ride public void drawListRow(ListField listField, Graphics g,int index, int y, int width)
and draw your own custom checkbox
To create an autocompletefield for OS4.5 onwards use the following code.
Vector box1 = new Vector();
// Create an instance of our SortedReadableList class.
MySortedReadableList mySortedReadableList= new MySortedReadableList (box1);
// Add our list to a KeywordFilterField object.
KeywordFilterField _keywordFilterField = new KeywordFilterField();
_keywordFilterField.setCallback(new ListFieldCallback() {
public void drawListRow(ListField listField, Graphics g,
int index, int y, int width) {
super.drawListRow(listField, g,
index, y, width);
}
public Object get(ListField listField, int index) {
if (index >= 0 && index < box1.size()) {
return _keywordFilterField.getResultList().getAt(index);
}
return null;
}
public int getPreferredWidth(ListField listField) {
return Display.getWidth();
}
public int indexOfList(ListField listField, String prefix,
int start) {
return listField.indexOfList(prefix, start);
}
});
_keywordFilterField.setSourceList(mySortedReadableList,
mySortedReadableList);
// We're providing a customized edit field for
// the KeywordFilterField.
CustomKeywordField customSearchField = new CustomKeywordField();
customSearchField.setPadding(8, 12, 8, 12);
_keywordFilterField.setKeywordField(customSearchField);
// Add our KeywordFilterField to the screen and push the screen
// onto the stack.
add(_keywordFilterField.getKeywordField());
add(_keywordFilterField);
definition of MySortedReadableList
class MySortedReadableList extends SortedReadableList implements KeywordProvider {
public MySortedReadableList (Vector box1) {
super(new MySortedReadableListComparator());
loadFrom(box1.elements());
}
void addElement(Object element) {
doAdd(element);
}
public String[] getKeywords(Object element) {
if (element instanceof String) {
return StringUtilities.stringToWords(element.toString());
}
return null;
}
final static class MySortedReadableListComparator implements Comparator {
public int compare(Object o1, Object o2) {
if (o1 == null || o2 == null) {
throw new IllegalArgumentException(
"Cannot compare null contacts");
}
return o1.toString().compareTo(o2.toString());
}
}
}
And now the CustomKeywordField
/**
* Inner Class: A custom keyword input field for the KeywordFilterField. We
* want to prevent a save dialog from being presented to the user when
* exiting the application as the ability to persist data is not relevent to
* this application. We are also using the paint() method to customize the
* appearance of the cursor in the input field.
*/
final static class CustomKeywordField extends BasicEditField {
// Contructor
CustomKeywordField() {
// Custom style.
super(USE_ALL_WIDTH | NON_FOCUSABLE | NO_LEARNING | NO_NEWLINE);
setLabel("Search: ");
setFont(boldTextFont);
}
/**
* Intercepts ESCAPE key.
*
* @see net.rim.device.api.ui.component.TextField#keyChar(char,int,int)
*/
protected boolean keyChar(char ch, int status, int time) {
switch (ch) {
case Characters.ESCAPE:
// Clear keyword.
if (super.getTextLength() > 0) {
setText("");
return true;
}
}
return super.keyChar(ch, status, time);
}
/**
* Overriding super to add custom painting to our class.
*
* @see net.rim.device.api.ui.Field#paint(Graphics)
*/
protected void paint(Graphics graphics) {
graphics.setColor(fontColor);
graphics.setFont(boldTextFont);
super.paint(graphics);
// Draw caret.
getFocusRect(new XYRect());
drawFocus(graphics, true);
}
}
}