blackberry add search field in list field

荒凉一梦 提交于 2020-01-16 14:44:10

问题


I want to add an Search Box in list Field. When i Enter a letter, then it will show the names starting with the letter 'A' , and so on. How its done ?. Iam using Vector to save the facebook friends list and di

splay it. Its not a normal list.
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);


}

回答1:


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);
    }
}

}



来源:https://stackoverflow.com/questions/8911157/blackberry-add-search-field-in-list-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!