gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:04:48 24 4
gpt4 key购买 nike

我想将一个字符串从 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);
}

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

enter image description here

最佳答案

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

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

它返回 GluonObservableObject<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();
}
});
}

这是此功能的快速实现:

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

关于android - 使用 gluon 移动插件在移动设备上存储字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42054447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com