views:

126

answers:

1

Hi, I am trying to display a list of data with checkboxes, where user can select multiple items at one time... I want a search field on top of the list which will search through the list...

How do I do it?

+2  A: 

Explanation

First, create a vector with all list fields, so you will be able to add and remove them on screen at any time. When you create screen, add them all to list control.

Now, lets say you have one of those EditFields - your search field. You can set a listener for it and catch text change event.

On that event you can remove all fields from list control, take text value from search field, iterate all fields vector and add those which meet search criteria to list control.

Code

LookupList control:

public class LookupList extends VerticalFieldManager {

    Vector mItems = null;

    public LookupList() {
     super();
     mItems = new Vector();
    }

    public void addItem(Field itemField) {
     String cookie = (String) itemField.getCookie();
     if (null == cookie)
      throw new IllegalArgumentException(
        "String cookie must be set to lookupList field");
     mItems.addElement(itemField);
     add(itemField);
    }

    public void removeItem(int index) {
     Field itemField = (Field) mItems.elementAt(index);
     mItems.removeElement(itemField);
     delete(itemField);
    }

    public void lookup(String searchValue) {
     deleteAll();
     for (int i = 0, count = mItems.size(); i < count; i++) {
      Field field = (Field) mItems.elementAt(i);
      String cookie = (String) field.getCookie();
      if (cookie.startsWith(searchValue))
       add(field);
     }
    }
}

Usage sample:

class Scr extends MainScreen implements FieldChangeListener {
    LookupList mList = null;
    BasicEditField mEdit = null;

    public Scr() {
     super();

     mEdit = new BasicEditField();
     add(mEdit);

     mEdit.setChangeListener(this);

     mList = new LookupList();
     for (int i = 0; i < 100; i++) {
      LabelField label = new LabelField("Lookup field #"
        + String.valueOf(i));
      label.setCookie(String.valueOf(i));
      mList.addItem(label);
     }

     add(mList);
    }

    public void fieldChanged(Field field, int context) {
     if (field == mEdit)
      mList.lookup(mEdit.getText());
    }
}

PS

  • you can add any Field descendend class to list (Edit/Label/CustomCheckBos etc), but it should have a string cookie with value to search.
  • you can change logic to search by Field label, text or custom property.
Max Gontar