2012-01-18 81 views
0

我想在列表字段中添加搜索框。當我輸入一個字母時,它會顯示以字母「A」開頭的名稱,依此類推。它是如何完成的? Iam使用Vector來保存Facebook的朋友列表,並將其展示爲enter image description here。它不是一個正常的列表。blackberry在列表字段中添加搜索字段

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

您可以使用自動填充字段,該字段可用於設備OS 5.0以上。如果你想你的應用程序開始對設備4.5Os工作,讓我知道,我會更新代碼

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的autocompletefield起使用下面的代碼。 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()); 
    } 
} 

}

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

定義和現在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); 
    } 
} 

}

+0

它應該在4.5工作onwords。我需要它與上面顯示的圖像相同。 – Signare 2012-01-18 14:12:10

+0

我編輯了答案。檢查一下。它沒有複選框字段,只是一串名字。 – rfsk2010 2012-01-18 14:22:25

+0

我也需要複選框。沒有複選框,我不能選擇多個項目。那麼如何添加複選框? – Signare 2012-01-19 06:22:56