简体   繁体   中英

Storing a string on mobile device using gluon mobile plugin

I want to save a string from a TextArea to the device and then reload it after reopening the app. I have tried following the examples ( link ) but cant get it to work. Main problem arises when i try to read the file and use a 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);
    }

Edit: inserted code which i tried to start reading file with and image of the error i am getting

在此输入图像描述

If you check the DataProvider::retrieveObject documentation:

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

It returns GluonObservableObject<String> , which is an observable wrapper of the string, not the string itself.

You need to get first the observable, and when the operation ends successfully you can retrieve the string:

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();
        }
    });
}

This is a quick implementation of this functionality:

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);
                }
            });
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM