使用 gluon 移动插件在移动设备上存储字符串

Storing a string on mobile device using gluon mobile plugin

我想将字符串从 TextArea 保存到设备,然后在重新打开应用程序后重新加载它。我尝试按照示例 (link) 进行操作,但无法正常工作。当我尝试读取文件并使用 StringInputConverter 时出现主要问题。

private void saveAndLoad(TextArea textArea){
        File textFile = new File(ROOT_DIR,"text_file");
        String text2 = textArea.getText();
        String loadedFile = "none";
        if (textFile.exists()){
            FileClient fileClient = FileClient.create(textFile);
        loadedFile = DataProvider.retrieveObject(fileClient.createObjectDataReader(
                new StringInputConverter()));
        }
        try(FileWriter writer = new FileWriter(textFile)){
            writer.write(textArea.getText());
        } catch (IOException e) {
            e.printStackTrace();
        }

        textArea.setText(text2);
    }

编辑:插入了我尝试开始读取文件的代码和我遇到的错误图像

如果您查看 DataProvider::retrieveObject 文档:

Retrieves an object using the specified ObjectDataReader. A GluonObservableObject is returned, that will contain the object when the read operation completed successfully.

它returns GluonObservableObject<String>,这是字符串的可观察包装,而不是字符串本身。

您需要先获取observable,当操作成功结束时您可以检索字符串:

if (textFile.exists()) {
    FileClient fileClient = FileClient.create(textFile);
    GluonObservableObject<String> retrieveObject = DataProvider
            .retrieveObject(fileClient.createObjectDataReader(new StringInputConverter()));

    retrieveObject.stateProperty().addListener((obs, ov, nv) -> {
        if (ConnectState.SUCCEEDED.equals(nv)) {
            loadedFile = retrieveObject.get();
        }
    });
}

这是此功能的快速实现:

public class BasicView extends View {

    private static final File ROOT_DIR;
    static {
        ROOT_DIR = Services.get(StorageService.class)
                .flatMap(StorageService::getPrivateStorage)
                .orElseThrow(() -> new RuntimeException("Error")); 
    }

    private final File textFile;
    private final TextField textField;
    private String loadedFile = "none";

    public BasicView(String name) {
        super(name);

        textFile = new File(ROOT_DIR, "text_file");
        textField = new TextField();

        VBox controls = new VBox(15.0, textField);
        controls.setAlignment(Pos.CENTER);
        controls.setPadding(new Insets(30));

        setCenter(controls);
    }

    @Override
    protected void updateAppBar(AppBar appBar) {
        appBar.setNavIcon(MaterialDesignIcon.MENU.button(e -> System.out.println("Menu")));
        appBar.setTitleText("Basic View");
        appBar.getActionItems().add(MaterialDesignIcon.SAVE.button(e -> save()));
        appBar.getActionItems().add(MaterialDesignIcon.RESTORE_PAGE.button(e -> restore()));
    }

    private void save() {
        try (FileWriter writer = new FileWriter(textFile)) {
            writer.write(textField.getText());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void restore() {
        if (textFile.exists()) {
            FileClient fileClient = FileClient.create(textFile);
            GluonObservableObject<String> retrieveObject = DataProvider
                    .retrieveObject(fileClient.createObjectDataReader(new StringInputConverter()));

            retrieveObject.stateProperty().addListener((obs, ov, nv) -> {
                if (ConnectState.SUCCEEDED.equals(nv)) {
                    loadedFile = retrieveObject.get();
                    textField.setText(loadedFile);
                }
            });
        }
    }
}