2012-01-18 7 views
0

リストフィールドに検索ボックスを追加します。 i文字を入力すると、文字 'A'で始まる名前が表示されます。どのように行われますか? Iamはベクトルを使用して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を作成するには、独自のカスタムチェックボックス

を描くことができます。

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

}

+0

はそれがonwords 4.5上で動作するはずです。上記の画像と同じものが必要です。 – Signare

+0

私は答えを編集しました。それを確認します。それはチェックボックスのフィールドを持っていませんが、名前の文字列です。 – rfsk2010

+0

チェックボックスも必要です。チェックボックスなしでは、1つ以上のアイテムを選択できません。どのようにチェックボックスを追加するのですか? – Signare

関連する問題