Search code examples
blackberryscrolllistfield

BlackBerry 6: how to scroll to a new item in KeywordFilterList?


the SearchFieldDemo works well for me, but has one problem: when you add a new country to the KeywordFilterList through the menu and that new item is on the bottom of the sorted list, then the user doesn't see anything and is unsure if the new item has been added or not.

A solution would be to make the KeywordFilterList scroll to the new item, but I can't figure out, how to do that. I've tried:

void addElementToList(Country country)
{       
    _countryList.addElement(country);
    _keywordFilterField.updateList();

    int index = _countryList.getIndex(country);
    System.err.println("XXX index: " + index);
    _keywordFilterField.setSelectedIndex(index);
}   

But this does not have any effect: the printed index is correct and the KeywordFilterList scrolls, but not to a correct spot.

Any ideas please? Alex


Solution

  • In the sample app you might have noticed the _keywordFilterField.setKeyword(""); line as the first thing they do before adding a new item to the list. This is to guarantee the new item will be visible in the list. Otherwise with some filter applied the list may not display the new item.

    So in your code it looks like you don't handle this point. As a result the index you get with int index = _countryList.getIndex(country); may not be the same as it is in the visible filtered by some current keyword list. BTW, to find the index in the visible list you could use the ReadableList which can be got with _keywordFilterField.getResultList().

    So the workflow could be as follows:

    1. reset keyword by _keywordFilterField.setKeyword(""); - now there is no filtering applied so the visible list should include a new item.
    2. add a new item to the underlying collection - _countryList.addElement(country);
    3. call _keywordFilterField.updateList(); to refresh the ListField.
    4. to be on the safe side find an index to scroll to by using the collection got by calling the _keywordFilterField.getResultList()
    5. select the new item with _keywordFilterField.setSelectedIndex(index);