Felipe
Felipe

Reputation: 537

Codename One - get selected text from AutoComplete

How can I get the complete selected text from an AutoComplete TextField?

If I use getText(), I only get the few letters the user has input so far.

Example: I write "flo" and then select "Flowers" from the list, but getText() gives me "flo"

AutoCompleteTextField auto = new AutoCompleteTextField(arrayWithNames);
auto.setMinimumLength(4);
auto.addListListener((ActionEvent evt1) -> {
    String lookedFor = auto.getText();
    Hashtable<String,Object> match[] = findMatch(lookedFor);
    if(hMatch.length>0){
        contElements.removeAll();
        for (Hashtable<String, Object> Match1 : match) {
            ...
            ...//fill the Container with the names found
            ...
        }
    }
});

How it works

I am using the AutoComplete TF as a search button. I have an array with all the names in my list. Then I populate the Auto with the array. The user selects a name from the Auto and then I search the value that is being "lookedFor" using the findMatch(). It returns a new array with the found entries.

I need the complete name from the list so I can use the findMatch() method, but when I use getText() from the Auto, it only returns the letters the user entered, and not the whole name, so my method does not work, since I am comparing whole Strings. (I am using the Auto because it is very convenient if people remember only a part of the name they are looking for)

Upvotes: 1

Views: 450

Answers (2)

Yngve Moe
Yngve Moe

Reputation: 510

If you subclass AutoCompleteTextField you can access the selected text internally via getSuggestionModel().getItemAt(getSuggestionModel().getSelectedIndex()). Now you can define a public getter method getSelectedText() or something on your derived class.

Upvotes: 1

zackery.fix
zackery.fix

Reputation: 1816

I am not sure you are using the AutoCompleteTextBox correctly.

The entire purpose of the AutoCompleteText box is to help you assist the user in selecting from a list of valid requests,

You should not be getting the value of getText() until the user is ready to submit the form where the AutoCompleteTB is located.

This WILL help if you haven't already looked here: https://www.codenameone.com/javadoc/com/codename1/ui/AutoCompleteTextField.html#getPropertyTypes--

Good luck!

Upvotes: 0

Related Questions