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
Contributor
nordavinder
Posts: 23
Registered: ‎12-02-2012
My Device: BlackBerry 8300
Accepted Solution

How to improve this search feature over a VerticalFieldManager that holds custom managers

I have a screen with a VerticalFieldManager for holding a list of custom managers.

 

The screen also contains a EditField that behaves as a search field, so when the user enters some text, my app loops over an array of custom managers and compares the entered text with the text from every manager in the array.

 

If the text from a manager matches the entered string, I append that custom manager to the VerticalFieldManager.

These are the relevant parts of my code :

 

EditField _editField;
TempBeanPlaces [] _placesList;
VerticalFieldManager _vfmCellPlaces;

 

....

This is the constructor of TemBeanPlaces :

 

public TempBeanPlaces(String pPlaceName,   CellPlaceManager pCellPlaceManager) {
       placeName = pPlaceName;
       cellPlaceManager = pCellPlaceManager;
}
...

 

And this codes handles the search :

_editField.setChangeListener(new FieldChangeListener() {
     public void fieldChanged(Field field, int context) {
         String text = _editField.getText().toLowerCase();
        _vfmCellPlaces.deleteAll();
        for (int i = 0; i < _placesList.length; i++) {
            TempBeanPlaces tempBeanPlaces = _placesList[i];
             if(tempBeanPlaces.getPlaceName().toLowerCase().indexOf(text) != -1) {
                 _vfmCellPlaces.add(tempBeanPlaces.getCellPlaceManager());
             }
        }
        updateLayout();
      }
});

 

This works ok when the array _placesList is not too large (length of 50, for example), otherwise the app becomes too slow.

 

How could I optimize my code for large amounts of custom managers?. For example, in case the array contains 600 elements.

 

I really need to improve this feature. I just have read about the ListField in BlackBerry, but since I have this code done, maybe I don't need to rewrite everything with ListFields.

 

Thanks in advance!

 

Please use plain text.
Developer
simon_hain
Posts: 16,190
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: How to improve this search feature over a VerticalFieldManager that holds custom managers

use a listfield. custom managers will always be slow as they need to do a lot of extra lifting for the layout.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
peter_strange
Posts: 19,607
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to improve this search feature over a VerticalFieldManager that holds custom managers

[ Edited ]

The biggest issue with updating Fields like this is the time to layout the Fields that are added.  If you add these one at time, then each add will provoke a layout.  Instead you can use addAll to add array of items, or alternatively, add all your items to a single Manager, then add the Manager.  Then the layout only occurs once. 

 

In your code, the option would be to add the selected Managers to a Vector, turn the Vector into an array, and then use addAll.

 

You do not need the updateLayout(). 

 

That said, the more Fields you add, the longer the layout processing will take anyway, so perhaps you will try to add more items than even this optimization will make run quickly.  If this optimization does not work enough, then you will need to look at a different approach, say 'paging' the results, so the screen only displays the first 25, and the user can 'add more'.  This 'paging' is a good approach in my experience because users will seldom scroll down 25, when they can do another search that might give them better results. 

 

Finally, and to tie in with Simon's comment, ListField is an extremely efficient Field because it is a single Field, so layout is easy, and it does not try to paint rows until these are actually displayed on the screen.  So in your case, you have three options:

 

1) addAll

2) paging

3) ListField

 

Let us know how you get on. 

Please use plain text.
Contributor
nordavinder
Posts: 23
Registered: ‎12-02-2012
My Device: BlackBerry 8300

Re: How to improve this search feature over a VerticalFieldManager that holds custom managers

Thanks again Peter!. I saw later your last suggestion, and ended up using the third option: ListField. Now I have a class that extends ListField and implements ListFieldCallback. The constructor takes an array of the custom Manager that renders the row, so in the drawListRow method I call a method from the mentioned Manager for drawing the row. Now, instead of populating the VerticalFieldManager as before, I just add my ListField class(with the updated array of results) to it. I hope I was clear. Maybe I give a try to your updated suggestion at night just for the sake of curiosity =) 

Please use plain text.
Developer
simon_hain
Posts: 16,190
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: How to improve this search feature over a VerticalFieldManager that holds custom managers

i would not suggest to use managers inside a listfield, you should implement the drawing yourself in drawListRow, it should be pretty straightforward (as far as manual graphics routines go...)
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.