Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 107
Registered: ‎08-08-2008
My Device: Not Specified

indexOfList for ListField search

I'm trying to create a search on a list field similar to the address book.

 

I have a BasicEdiField as the input with a FieldListener. All of this is in a Dialog screen.

It's always returns 0xFFFF. When i step through the codeI notice that the _itemListField value is NULL and yet its displayed and filled with items.

 

    class MyChangeListener implements FieldChangeListener
    {
         public void fieldChanged(Field field, int context)
         {
       
        if ( field == findItem )
            {
                String tmp = findItem.getText();               
                int n = indexOfList(_itemListField, tmp, 1);
            }
        };
    }
 

Am i missing something?

 

Thanks.
t

 

 

 

Developer
Posts: 107
Registered: ‎08-08-2008
My Device: Not Specified

Re: indexOfList for ListField search

another rookie mistake. I forgot to implement the indexOfList method in my callback.

New Developer
Posts: 1
Registered: ‎01-29-2009
My Device: Not Specified

Re: indexOfList for ListField search

Can you tell me how did you manage to get it,I have been trying to accomplish this searching functionality but ListOfIndex methods always return -1,below is my sample of code

 

 

 AddressBook()
  {
    _addContact = new ADD();
    _table =new Vector();
 
    
   // setStatus(listField.getSearchBar())
   //  LabelField lbl = new LabelField("Address Book     ".concat(navLabel), 0, -1, Field.FIELD_HCENTER
     //);    
    
     listField = new ListField();
     listField.setCallback(this);
     listField.setFocusListener(this);
     //listField.setSearchable(true);
    
       editField = new BasicEditField("Find: ", "")
    {
        public void paint(Graphics graphics) {
        graphics.setBackgroundColor(0xCCFF66);
        graphics.drawRect(0,15,35,1);
        graphics.clear();
        super.paint(graphics);
    }

        };
        editField.setChangeListener(this);
        editField.setFocusListener(this);

     add(editField);
     add(listField);  
     reloadContactList();    
}
public boolean onSavePrompt()
{
    return true;
}
public void focusChanged(Field field, int eventType) {
/*if(field ==editField)
Dialog.inform("focus editField");
if(field ==listField)
Dialog.inform("Focus listField");
*/
}

int sortIndex=0;
//String entry;
public void fieldChanged(Field field, int context)
    {
       
        if(field==editField)
        {
        String prefix = editField.getText();
       
       /* for (int a=0; a<_table.size();a++)
        {        
         Object obj= _table.elementAt(a);
         if(obj instanceof String)
         {
         String entry  =(String)obj;   
          if(entry.startsWith(prefix));
          {
          sortIndex=_table.indexOf(entry);            
          break;
          //
          }
         }
                }*/
                int n=indexOfList(listField,prefix,1);             
                 Dialog.inform(" "+n);
                 //   listField.invalidate();
        }
    }
    
 
  private boolean reloadContactList()
  {
    try {
      blackBerryContactList =
        (ContactList)PIM.getInstance().openPIMList

          (PIM.CONTACT_LIST, PIM.READ_ONLY);
   
      Enumeration allContacts = blackBerryContactList.items();
      blackBerryContacts = enumToVector(allContacts);
      listField.setSize(blackBerryContacts.size());

      return true;
    }
    catch (PIMException e)
    {
      return false;
    }
  }

  //Convert the list of contacts from an Enumeration to a Vector
  private Vector enumToVector(Enumeration enum1) {
    Vector v = new Vector();

    if (enum1 == null)
      return v;

    while (enum1.hasMoreElements()) {
      v.addElement(enum1.nextElement());
    }

    return v;
  }
   int move=0;
  //ListFieldCallback methods
  public void drawListRow(
    ListField fieldVar,   
    Graphics graphics,
    int index, int y, int width
  ){
   
    if ( listField == fieldVar
    && index < blackBerryContacts.size())
    {
      blackBerryContact =
        (BlackBerryContact)blackBerryContacts.elementAt(index);

      String[] name = blackBerryContact.getStringArray(Contact.NAME, 0);

     
      boolean found = false;
     
      String nameseg;
      String prefix;
      String givenName;
      String familyName;
      StringBuffer sb = new StringBuffer();
      if ((prefix = name[Contact.NAME_PREFIX]) != null) {
      sb.append(prefix);
      
        found = true;
      }    
       if ((givenName = name[Contact.NAME_GIVEN]) != null) {
        if (found) {
          sb.append(' ');
        
           }
        sb.append(givenName);              
                  
        found = true;
      }
      if ((familyName = name[Contact.NAME_FAMILY]) != null) {
       if (found) {
        sb.append(' ');       
        }
        sb.append(familyName);
        
       
      }
      _table.insertElementAt(sb.toString(),move);
      graphics.drawText(sb.toString(), 0, y, 0, width);
    }  
    move++;
  } 

  public Object get(ListField fieldVar, int index)
  {
    if (listField == fieldVar)
    {
      //If index is out of bounds an exception will be thrown,
      //but that's the behaviour we want in that case.
      return blackBerryContacts.elementAt(index);     
    }
    return null;
  }

  public int getPreferredWidth(ListField fieldVar)
  {
    //use all the width of the current LCD
    return Graphics.getScreenWidth();
  }

  public int indexOfList
    (ListField fieldVar,String prefix,int start)
  {
    return listField.indexOfList(prefix, start);

  }

//Setup the menu.
  protected void makeMenu(Menu menu, int instance)
  {
    menu.add(_addContact); 
    super.makeMenu(menu, instance);
  }
 Vector _phone;
 
 public Vector getPhone(BlackBerryContact bbContact)
 {
     _phone =new Vector();
    int fieldCount, atrCount;  
   
    int fieldsWithData[] = bbContact.getFields();
         String phone;
         String prefix;
    for ( fieldCount = 0;
          fieldCount < fieldsWithData.length;
          ++fieldCount)
    {
    switch (fieldsWithData[fieldCount])
    {
      case BlackBerryContact.TEL:
          for (atrCount = 0;atrCount < bbContact.countValues(BlackBerryContact.TEL);++atrCount)
          {
            switch(bbContact.getAttributes(BlackBerryContact.TEL, atrCount))
            {
              case BlackBerryContact.ATTR_WORK:
              phone=bbContact.getString(BlackBerryContact.TEL,atrCount);
              if(phone!=null)
              {
                  prefix ="WORK:";
                if(prefix.concat(phone)!="")
              _phone.addElement(prefix.concat(phone));
               }
               //(BlackBerryContact.TEL, atrCount);
                break;       
              case BlackBerryContact.ATTR_HOME:
                     phone=bbContact.getString(BlackBerryContact.TEL, atrCount);
                     if(phone!=null)
                     {
                        
                      prefix ="HOME:";
                      if(prefix.concat(phone)!="")
                     _phone.addElement(prefix.concat(phone));
                     }
                  //(BlackBerryContact.TEL, atrCount);
                break;           
               
              case BlackBerryContact.ATTR_MOBILE:
               //_phone=bbContact.getString
                 // (BlackBerryContact.TEL, atrCount);
                 phone=bbContact.getString(BlackBerryContact.TEL, atrCount);
                 if(phone!=null)
                 {
                  prefix ="MOB:";
                  if(prefix.concat(phone)!="")
                 _phone.addElement(prefix.concat(phone));
                 }
                break;
                     }
           }           
      }
    } 
    return _phone;  
}

 

Is there something wrong in above code?

BlackBerry Development Advisor
Posts: 15,814
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: indexOfList for ListField search

From the JavaDocs:

 

Results of the search (see description); if the start parameter is larger than the number of items in this list, this method returns -1.

 

 

 

I recommend you step through your code to verify that you are not sending an invalid start parameter.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
New Contributor
Posts: 2
Registered: ‎11-18-2011
My Device: 9700
My Carrier: digicel

Re: indexOfList for ListField search

if it is that you are using a dialog and you added an editfield to it the the index of the edit field should bbe zero (0) and not one (1)

 

 class MyChangeListener implements FieldChangeListener
{
public void fieldChanged(Field field, int context)
{

if ( field == findItem )
{
String tmp = findItem.getText();
int n = indexOfList(_itemListField, tmp, 1);//1 should be 0
}
};
}

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: indexOfList for ListField search

My understanding is that the default implementation of indexOfList will do a 'toString()' after doing a get to act as the 'search' text, will only search forward and will stop at the first match on the first character.  But I could be wrong and I haven't looked recently.  If you are displaying a complex Field (not a String) then this might not be what you want.  In this case I would recommend implementing your own indexOfList to search as you want to. 

Highlighted
Contributor
Posts: 15
Registered: ‎04-27-2011
My Device: Storm 2-9550
My Carrier: Verizone

Re: indexOfList for ListField search

In this case i think you should use the KeywordFilterField with the build-in search field which can be get and add to manager as any other custom search field

 

TextField _searchField = _myList.getKeywordField(); // with _myList is a KeywordFilterField