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[]{});
}
}