猿问

使用来自 Google API 的数据在文本字段中自动建议

我想制作一个带有建议的自动完成文本字段,其中数据来自 Google API - 在按下每个新键后更新。现在,我有一种方法可以下载 5 个建议并在按下另一个键时更新它们。


我试过AutoCompleteTextFieldGluon,但效果不佳。


public class Controller {

    Weather weather = new Weather();

    GooglePlaces googlePlaces = new GooglePlaces();


    @FXML

    AutoCompleteTextField<String> autoCompleteTextField = new AutoCompleteTextField<>();


    @FXML

    public void setAutoComplete() throws IOException {

        ArrayList<String[]> places = googlePlaces.predictPlaces("New yo");


        autoCompleteTextField.setCompleter(s -> {

            ArrayList<String> autoplaces = new ArrayList<>();

            for (int i = 0; i < places.size(); i++) {

                autoplaces.add(places.get(i)[0]);

            }

            System.out.println("test");

            return autoplaces;

        });

    }

}

在这里,我尝试从"New yo"阶段添加 5 个建议,而不是在每个新密钥之后更新,但它也没有用,因为它没有显示任何内容。"test"未打印在控制台中。


宝慕林4294392
浏览 111回答 1
1回答

杨__羊羊

在我看来,您需要调用setCompleter()一个名为的方法initialize():public class Controller {&nbsp; &nbsp; @FXML&nbsp; &nbsp; public void initialize() {&nbsp; &nbsp; &nbsp; &nbsp; autoCompleteTextField.setCompleter(input -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<String[]> places = googlePlaces.predictPlaces(input);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答