index of cursor in dataChanged() always returns -1

13 views
Skip to first unread message

orme...@gmail.com

unread,
Jun 1, 2015, 9:36:50 AM6/1/15
to codenameone...@googlegroups.com
I'm experiencing this problem in the simulator, I haven't yet tested it on an actual device.
I have a custom Component that overrides AutocompleteTextField.  I'm not interested in displaying suggestions unless at least 2 characters have been inputted into the text field, but the suggestions never display at all.  When I debugged, I discovered that the index param in the fireDataChanged() method is always returning -1, no matter how many characters have been inputted.  Is there some other way to go around this?

As well I was having a problem that I populate the suggestions ListModel only after data is inputted, and I was getting a nullPointerException because getSuggestionModel was being called during initialization.  So I was storing the cursor index and returning a ListModel populated with and empty array unless the cursor had passed position 0.  Is there a better way to do this, especially since cursor position is always being returned as -1?

Here's some code:
public class ForumNamesAutocomplete extends AutoCompleteTextField {
    List<String>suggestions;
    List<Map<String,Object>> fData;
    StateMachine mac;
    int currentIndex;
    public static final String KEY_FORUM_NAME = "forum name";
    public static final String KEY_FORUM_ID = "forum id";
    public static final String KEY_FORUM_DESC = "forum desc";
    
    public ForumNamesAutocomplete(StateMachine sm){
        super();
        mac = sm;
        if(sm.forumData != null){
            fData = mac.forumData;
        }
    }

@Override
    public void fireDataChanged(int type, int index) {
        currentIndex = index;
        if(index > 0){
        if(fData == null){
            fData = mac.forumData;
        }
        setSuggestionList(this.getText());
        super.fireDataChanged(type, index);
        }
    }

@Override
    protected ListModel<String> getSuggestionModel() {
        if (currentIndex > 0) {
            return new DefaultListModel<String>(suggestions) {};
        } else {
            return new DefaultListModel<String>(new String[]{});
        }
    
    }

Shai Almog

unread,
Jun 1, 2015, 11:16:22 AM6/1/15
to codenameone...@googlegroups.com, orme...@gmail.com, orme...@gmail.com
Why not just use setMinimumLength(2)?

orme...@gmail.com

unread,
Jun 2, 2015, 3:01:35 AM6/2/15
to codenameone...@googlegroups.com, orme...@gmail.com
I changed my code to use your suggestion, but still fireDataChanged() is being called right away, and it's giving a nullPointerException() because the suggestions list only gets initiated in the line after that in the constructor.  Is fireDataChanged() supposed to be called even if it the minimum length hasn't been reached?   What other way is there to solve this?

Thanks 
Reply all
Reply to author
Forward
0 new messages