【问题标题】:How to access saved data from different method?如何从不同的方法访问保存的数据?
【发布时间】:2015-01-11 23:39:29
【问题描述】:

我正在尝试将一个值作为字符串保存在设备的内存中,以便在应用程序关闭并通过单击另一个按钮重新打开时可以访问它。当我运行程序时,我为输入 A 和 B 输入值,我知道它会通过计算处理它们,因为我对其进行了修改,以便它在计算后立即显示答案。 但是在这个版本中,如果我单击保存按钮,然后单击访问按钮以显示答案和标记为上一个答案的文本视图,它只会显示“xx”,这是我试图保存的字符串的初始值。所以要么它不存储包含答案的更新版本,要么访问按钮只能访问字符串的原始值。

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

    );

【问题讨论】:

  • 不确定,至于您的文件处理...我太新了。但是,你为什么不使用共享偏好?这似乎是他们使用的教科书案例。

标签: java android string methods save


【解决方案1】:

共享偏好如何?这是一个例子。

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

【讨论】:

    【解决方案2】:

    Android 为您提供了多种保存持久应用程序数据的选项。您选择的解决方案取决于您的特定需求,例如数据是否应该对您的应用程序私有或可供其他应用程序(和用户)访问,以及您的数据需要多少空间。

    您的数据存储选项如下:

    共享偏好 将私有原始数据存储在键值对中。

    内部存储 将私有数据存储在设备内存中。

    外部存储 将公共数据存储在共享的外部存储上。

    SQLite 数据库 将结构化数据存储在私有数据库中。

    网络连接 使用您自己的网络服务器将数据存储在网络上。

    对于您给定的应用程序,我认为 Shared PreferencesInternal Storage 将是可行的方法。

    内部存储:使用此路由,您必须调用 openFileOutput() 来启动 FileOutputStream,然后从那里使用 write() 将数据写入文件,close() 关闭 FileOutputStream

    例如:

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

    SharedPreferences:如果你要使用这个,那么你必须调用edit()来得到一个SharedPrefrencesEditor,然后添加为此,您只需使用 putBoolean()putString() 等。完成后,您可以使用 commit() 提交数据em>。

    例如:

    //Initiate Shared Preferences
    SharedPreferences.Editor editor = settings.edit();
    
    //Writes data
    editor.putBoolean("silentMode", mSilentMode);
    
    // Commit the edits!
    editor.commit();
    

    (Source)

    【讨论】:

      猜你喜欢
      • 2013-05-19
      • 1970-01-01
      • 2015-08-17
      • 1970-01-01
      • 2013-05-08
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      相关资源
      最近更新 更多