我想在列表字段中添加搜索框。当我输入一个字母时,它将显示以字母“A”开头的名称,依此类推。怎么做的?我使用Vector来保存facebook好友列表,并且di 显示它。这不是一个正常的清单。
Vector box1 = new Vector();
for(int i=0;i<splash.vector.size();i++){
FriendsRequestObject co_vec = (FriendsRequestObject)splash.vector.elementAt(i);
String name=co_vec.getSender_name();
String id=co_vec.getSender_id();
//Dialog.alert(""+name);
box = new CheckboxField(" "+name , checked, Field.USE_ALL_WIDTH){
public void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
super.paint(graphics);
}
};
box1.addElement(box);
// box.setMargin(6, 0, 0, 4);
vfm.add(box);
}
答案 0 :(得分:1)
您可以使用自动填充字段,该字段可在设备OS 5.0之后使用。如果您希望您的应用程序在设备上运行4.5O,请告诉我,我将更新代码
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);
此代码将列出向量中的所有字符串,并在您键入时过滤结果。
如果您想绘制复选框,可以覆盖public void drawListRow(ListField listField, Graphics g,int index, int y, int width)
并绘制自己的自定义复选框
要为OS4.5创建自动填充字段,请使用以下代码。
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);
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());
}
}
}
现在是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);
}
}
}