Java 如何从不同的方法访问保存的数据?
我正在尝试将一个值保存为设备内部内存中的字符串,以便在关闭应用程序并通过单击另一个按钮重新打开应用程序时可以访问该值。当我运行程序时,我为输入A和B输入值,我知道它通过计算来处理它们,因为我修改了它,以便它在计算后立即显示答案。 但是在这个版本中,如果我单击save按钮,然后单击Access按钮来显示答案和标有Previous answer的textview,它只会显示“xx”,这是我试图保存的字符串的初始值。因此,要么它不存储包含答案的更新版本,要么Access按钮只能访问字符串的原始值Java 如何从不同的方法访问保存的数据?,java,android,string,methods,save,Java,Android,String,Methods,Save,我正在尝试将一个值保存为设备内部内存中的字符串,以便在关闭应用程序并通过单击另一个按钮重新打开应用程序时可以访问该值。当我运行程序时,我为输入A和B输入值,我知道它通过计算来处理它们,因为我修改了它,以便它在计算后立即显示答案。 但是在这个版本中,如果我单击save按钮,然后单击Access按钮来显示答案和标有Previous answer的textview,它只会显示“xx”,这是我试图保存的字符串的初始值。因此,要么它不存储包含答案的更新版本,要么Access按钮只能访问字符串的原始值
Button jSave = (Button) findViewById(R.id.iSave);
Button jAccess = (Button) findViewById(R.id.iAccess);
final String saveName="Name";
final String saveValue = "xx";
jSave.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
//Calculations. These are a part of a more complex series of
//calculations between several classes, but I've simplified it
//somewhat for this post.
EditText jInputA = (EditText)findViewById(R.id.iInputA);
double dInputA = Double.parseDouble(jInputA.getText().toString());
EditText jInputB = (EditText)findViewById(R.id.iInputB);
double dInputB = Double.parseDouble(jInputB.getText().toString());
double myAnswer = Double.parseDouble(ProfileCalculations.functionQ(jInputA, jInputB));
//Update the value of saveValue to match that of myAnswer
final String saveValue = "The answer is " myAnswer;
//Save saveValue as a string under file saveName
try{
FileOutputStream jFOS = openFileOutput(saveName, Context.MODE_PRIVATE);
jFOS.write(saveValue.getBytes());
jFOS.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Profile.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Profile.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
);
jAccess.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
try {
FileInputStream jFIS = openFileInput(saveName);
jFIS.read(saveValue.getBytes());
jFIS.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Profile.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Profile.class.getName()).log(Level.SEVERE, null, ex);
}
TextView jPreviousAns = (TextView) findViewById(R.id.iPreviousAns);
jPreviousAns.setText(saveValue + "");
}
}
);
共享偏好如何?这里有一个例子
private SharedPreferences defaultPrefs;
defaultPrefs = PreferenceManager.getDefaultSharedPreferences(this);
String url = "http://www.example.com";
SharedPreferences.Editor ed = defaultPrefs.edit();
ed.putString("homepage", url);
ed.commit();
后来:
String url = defaultPrefs.getString("homepage", "http://www.example.com/some_default_page");
Android为您提供了几个保存持久应用程序数据的选项。您选择的解决方案取决于您的特定需求,例如数据是应用程序的私有数据还是其他应用程序(和用户)可以访问的数据,以及您的数据需要多少空间 您的数据存储选项如下: 共享首选项 在键值对中存储私有基元数据 内部存储 在设备内存中存储私有数据 外部存储 将公共数据存储在共享的外部存储器上 SQLite数据库 将结构化数据存储在专用数据库中 网络连接 使用您自己的网络服务器在web上存储数据 对于您给定的应用程序,我认为共享首选项或内部存储将是一种选择 内部存储:使用此路由,您必须调用openFileOutput()来启动FileOutputStream,然后使用write()将数据写入文件,close()关闭FileOutputStream 例: 共享首选项:如果要使用此选项,则必须调用edit()获取SharedPreferenceEditor,然后要向其添加值,只需使用putBoolean()、putString()等。完成后,使用commit()提交数据 例:
不确定,关于您的文件处理。。。我太新了。但是,为什么不使用共享首选项呢?这似乎是他们使用的教科书案例。
String FILENAME = "hello_file";
String string = "hello world!";
//Initiate
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
//Write to file
fos.write(string.getBytes());
//Close file
fos.close();
//Initiate Shared Preferences
SharedPreferences.Editor editor = settings.edit();
//Writes data
editor.putBoolean("silentMode", mSilentMode);
// Commit the edits!
editor.commit();