-1

I 'm testing with some iteration with the coding below, with help of Stringbuilder and want to directly save the outcome to Sharedpreference.

save2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick (View v){
        int i;
        int n = 10;

        StringBuilder outoutcome = new StringBuilder();
        for (i = 0; i <= n; i++) {
            outoutcome.append(i + "\n");
        }

        SharedPreferences sharedPreferences = getSharedPreferences("data1", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("outcome1", outoutcome.toString());
    }
}

*I also tried editor.putString("outcome1", String.valueOf(outoutcome)); but unfortunately cannot.

for the retrieve of data in another activity, I had tried using coding below :

public static final String DEFAULT = "";
final SharedPreferences sharedPreferences = getSharedPreferences("data1", Context.MODE_PRIVATE);
final String out1 =sharedPreferences.getString("outcome1", DEFAULT);
resultout.setText("Saved data is " + out1 );

but the coding above not working. Then, I searched and found this , tried the code as below but still not working.

 final String[] getout1= out1.split(",");
 resultout.setText("Saved data is " + getout1 );

Can anyone help here? Thank you in advanced !

Community
  • 1
  • 1
GentleG
  • 49
  • 1
  • 9

1 Answers1

1

You have to use apply() or commit() to save the changes.

editor.apply();

or

editor.commit();
Joshua
  • 4,757
  • 1
  • 23
  • 45
  • Ya , just found that forgotten the editor.commit() . Thought other things causes this problem and lost the focus on this. Thanks. – GentleG Jul 05 '16 at 06:45